sonjasan
sonjasan

Reputation: 49

Mongodb Update 'set' without deleting old one

Below is the query that updates my cart based on userId, what happpens is that whenever I update the cart, the old ones is being cleared. What I want to happen is that, whenever I update the cart, I will just update the cart contents.

Query :

await db.grocerylst.updateOne({ userId: req.session.custid }, {
    $set: { cart: req.session.cart }
}, { upsert: true });   

db data sample :

/* 1 */
{
    "_id" : ObjectId("5e9542a3deba15a3115d421d"),
    "userId" : ObjectId("5e5e12e1c6b1ed140cfc44b8"),
    "cart" : {
        "11f3eba8c2168a485ceba687d7c07571bb7633b8" : {
            "productId" : ObjectId("5e2a85b6d052e12078d40418"),
            "title" : "JOHNNIE WALKER® PLATINUM LABEL™ 18 YEAR OLD",
            "quantity" : 1,
            "totalItemPrice" : 3798,
            "options" : {},
            "productImage" : "/uploads/5e2a85b6d052e12078d40418/01-johnnie-walker-platinum-18-year-old.png",
            "productComment" : null,
            "productSubscription" : null,
            "productFrom" : {
                "_id" : "5e2a4e78d38f491d0082b949",
                "storeName" : "Tiongsan Supermarket",
                "storeLocation" : "Baguio City"
            },
            "link" : "Johnywalkerplatinum"
        },
        "d57a5067dda49d2228fde6b4aea84eae43481007" : {
            "productId" : ObjectId("5e2a8627db85f82f30188554"),
            "title" : "JOHNNIE WALKER® BLACK LABEL®",
            "quantity" : 1,
            "totalItemPrice" : 1140,
            "options" : {},
            "productImage" : "/uploads/5e2a8627db85f82f30188554/01-johnnie-walker-black-label.png",
            "productComment" : null,
            "productSubscription" : null,
            "productFrom" : {
                "_id" : "5e2a4e78d38f491d0082b949",
                "storeName" : "Tiongsan Supermarket",
                "storeLocation" : "Baguio City"
            },
            "link" : "Johnywalkerblack"
        },
        "80fc5143978c39b00761d1b07023bb1dc3d8b42a" : {
            "productId" : ObjectId("5e2a86a8db85f82f30188555"),
            "title" : "JOHNNIE WALKER® BLUE LABEL™",
            "quantity" : 1,
            "totalItemPrice" : 8500,
            "options" : {},
            "productImage" : "/uploads/5e2a86a8db85f82f30188555/01-johnnie-walker-blue-label.png",
            "productComment" : null,
            "productSubscription" : null,
            "productFrom" : {
                "_id" : "5e2a639744360f00dca93a9d",
                "storeName" : "SM Supermarket",
                "storeLocation" : "Baguio City"
            },
            "link" : "Johnywalker"
        }
    }
}

Upvotes: 2

Views: 1040

Answers (1)

whoami - fakeFaceTrueSoul
whoami - fakeFaceTrueSoul

Reputation: 17925

When you do $set: { cart: req.session.cart } it will replace the cart field with new data from req.session.cart, So if you wanted to add new nested objects to cart object then it has to be like :

/** So 'SomeID' has to be an id like '11f3eba8c2168a485ceba687d7c07571bb7633b8' */
await db.grocerylst.updateOne({ userId: req.session.custid }, {
    $set: { 'cart.SomeID': req.session.cart }
}, { upsert: true }); 

Or if cart is an array like cart :[{},{},{}], then you need to use $push :

/** Pushes new objects to existing 'cart' array or will create new 'cart' array field if it doesn't exists */
await db.grocerylst.updateOne({ userId: req.session.custid }, {
    $push: { 'cart': req.session.cart }
}, { upsert: true }); 

Upvotes: 1

Related Questions