最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

node.js - mongodb push update based on ne and arrayfilters not working - Stack Overflow

programmeradmin3浏览0评论

I'm trying to create a patch API for a database of products in a MERN application that keeps track of expiry dates.

The idea is when a product's UPC is given, as well as the date in which it expires, the specific product is updated with the date given added to expiry dates, provided the date isn't in there already.

Sample data:

[
{
     "_id":{"$oid":"6795e982c4e5586be7dc5bfc"},
     "section":"Dairy",
     "products":
     [
          {
               "productUPC":"068700115004",
               "name":"Dairyland 2% Milk Carton 2L",
               "expiryDates":
               [
                    {
                         "dateGiven":"2025-01-30T00:00:00.000-06:00",
                         "discounted":true
                    }
               ]
          },
          {
               "productUPC":"068700011825",
               "name":"Dairyland 1% Milk Carton 2L",
               "expiryDates":[
               ]
          }
     ]
}
]

The following is the node for the patch API:

router.patch("/products/:productUPC&:expiryDate", async (req, res) => {
    try {
        let collection = await db.collection("storeSections");
        let result = await collection.updateOne({
          "products.expiryDates.dateGiven": {
            $ne: new Date(moment(req.params.expiryDate)).toISOString(true)
          }
          },
          {
            $push: {
              "products.$[x].expiryDates": {
                "dateGiven": new Date(moment(req.params.expiryDate)).toISOString(true),
                "discounted": false
              }
            }
          },
          {
            arrayFilters: [
              {
                "x.productUPC": req.params.productUPC
              }
            ]
          })
          
        res.send(result).status(200);
    } catch(err) {
        console.error(err);
        res.status(500).send("Error updating record.");
    }
});

And here is a sample patch that I sent with Postman:

localhost:5050/record/products/productUPC=068700115004&expiryDate=2025-02-21

The update worked just fine in a mongoDB playground... why isn't it working now?

发布评论

评论列表(0)

  1. 暂无评论