0

I need to be update element inside product array called "available" which is inside an another array called cartItem. Below is my document structure and query params are _id , cartItem.storeId and cartItem.products._id. Help will be much appreciated

{
    "_id" : ObjectId("5fa3adffc59bf52b0c8583d7"),
    "totalAmount" : "40",
    "cartItem" : [ 
        {
            "_id" : ObjectId("5fa3adffc59bf52b0c8583d8"),
            "storeId" : "5f9c1cbd95252731bc90c2c8",
            "storeName" : "Ashapura General Stores",
            "storeMobile" : "9987177365",
            "products" : [ 
                {
                    "_id" : "5f9c1d7795252731bc90c2ca",
                    "title" : "Bourbon",
                    "quantity" : "1",
                    "price" : "20",
                    "available" : "yes"
                }, 
                {
                    "_id" : "5f9c1dca95252731bc90c2cc",
                    "title" : "Oreo",
                    "quantity" : "1",
                    "price" : "20",
                    "available" : "yes"
                }
            ]
        }
    ],
    "address" : "804/2 Sarvoday leela,90 feet road, kanchangaon, Thankurli,mumbai",
    "mobile" : "9987177365",
    "customerID" : "5f8d3eee9083492d34da7185",
    "time" : ISODate("2020-11-05T07:47:11.585Z"),
    "status" : "pending",
    "__v" : 0
}
chridam
  • 100,957
  • 23
  • 236
  • 235
  • Welcome to Stackoverflow! Can you please [edit] your question to include the query involved i.e. which specific `cartItem` array element and or `product` array element? – chridam Nov 17 '20 at 15:55
  • it will depend on storeId in cartItem and _id in products, basically query will be combination of _Id, cartItem.storeId and cartItem.products._id. – Keith kristopher D'souza Nov 17 '20 at 16:18
  • From any of the marked duplicate answers, use [arrayFilters](https://docs.mongodb.com/manual/release-notes/3.6/#arrayfilters) and [`$[]`](https://docs.mongodb.com/manual/reference/operator/update/positional-filtered/#up._S_[%3Cidentifier%3E]) as `db.collection.update( { "_id" : <_id> }, { "$set": { "cartItem.$[elem].products.$[productField]._id": } }, { arrayFilters: [ {"elem.storeId": }, { "productField.available": "no" } ] } )` – chridam Nov 17 '20 at 16:34

0 Answers0