简体   繁体   中英

Mongoose update an object from an array?

I am trying to updating my mongoose data-

Here is data structure-

在此处输入图像描述

Now I have to update products quantity and price.

Here is my practice-

const result = await Cart.findOneAndUpdate({
      products: {
       $elemMatch: {
         product: input.product
       }
      }
  }, {
     "products.$.quantity": // Here should be increase 1 with previous value. And when reached 5 then should not updated & give a error message
     "product.$.price": // Products should be incresed with previous price. example if previous price is 450. then it should be 900, 1350, everytime it increased 450.
  })
db.collection.update({
  ...filter
},
{
  $inc: {
    "products.$[].quantity": 1
  }
})

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM