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
}
3
  • 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? Commented Nov 17, 2020 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. Commented Nov 17, 2020 at 16:18
  • From any of the marked duplicate answers, use arrayFilters and $[<identifier>] as db.collection.update( { "_id" : <_id> }, { "$set": { "cartItem.$[elem].products.$[productField]._id": <productId> } }, { arrayFilters: [ {"elem.storeId": <storeId> }, { "productField.available": "no" } ] } ) Commented Nov 17, 2020 at 16:34

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.