简体   繁体   中英

How to updated the lastly added data to a array of objects in mongo db

Here is my data

{
"userId": "xyzzzz",
"items": [{
    "addedToCartOn": 1536131159,
    "itemName": "Chicken Sausage Pizza (Personal)",
    "sku": "Ch-Pi-No-No-rIotXAkFXe",
    "addOnsAdded": 1,
    "quantity": 1
}, {
    "addedToCartOn": 1536131159,
    "itemName": "Chicken Sausage Pizza (Personal)",
    "sku": "Ch-Pi-No-No-rIotXAkFXe",
    "addOnsAdded": 1,
    "quantity": 1
}]
 }

Now i want to increase the "quantity" of the data which is lastly added to the items array.

You can try this if you know the index of the last item. In this case it is 1

db.collection.update({"userId": "xyzzzz"},{$set:{"items.1.quantity":2}})

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