Sanaullah Ahmad
Sanaullah Ahmad

Reputation: 508

Update a nested value in mongodb

I am facing problem in updating a value inside product_details information. i send reservation id, and Ids of products inside product_details information, whose "is_picked" value need to be marked "true. I have a reservation table in which i am saving my reservation. here is its schema

{ 
"_id" : ObjectId("5b2a56c165cb721d8036923e"), 
"guest_details" : {
    "name" : "sanaullahAhmad", 
    "contact_number" : "0332695258"
}, 
"status" : "pending", 
"products_details" : [
    {
        "is_picked" : false, 
        "_id" : ObjectId("5b2a56c165cb721d80369240"), 
        "product" : {
            "in_stock" : true, 
            "special_offers" : "This is special offer", 
            "is_active" : true, 
            "_id" : ObjectId("5b10dc0aa5d60c23a8947e7a"), 
            "date_registered" : ISODate("2018-06-01T05:39:22.581+0000"), 
            "medicine_name" : "Panadol syrup", 
            "drug_reg_num" : "1236545", 
            "vendor_id" : "5b11212d4405ce1b30663152", 
            "strength" : "32", 
            "strength_unit" : "mm", 
            "price" : "225", 
            "volume" : "456", 
            "volume_unit" : "kilogrames", 
            "category" : "Capsules", 
            "active_substance_1" : "active_substance_1", 
            "active_substance_2" : "active_substance_2", 
            "active_substance_3" : "active_substance_4", 
            "active_substance_4" : "", 
            "mah_name" : "this is mah name", 
            "__v" : NumberInt(0)
        }, 
        "qty" : NumberInt(15)
    }, 
    {
        "is_picked" : false, 
        "_id" : ObjectId("5b2a56c265cb721d80369241"), 
        "product" : {
            "in_stock" : true, 
            "special_offers" : "This is special offer", 
            "is_active" : true, 
            "_id" : ObjectId("5b0d4c9abcd16f0558afce85"), 
            "date_registered" : ISODate("2018-05-29T12:50:34.299+0000"), 
            "medicine_name" : "Sirgical spirit", 
            "drug_reg_num" : "1236545", 
            "vendor_id" : "5b11212d4405ce1b30663152", 
            "strength" : "32", 
            "strength_unit" : "mm", 
            "price" : "225", 
            "volume" : "456", 
            "volume_unit" : "kilogrames", 
            "category" : "Capsules", 
            "active_substance_1" : "active_substance_1", 
            "active_substance_2" : "active_substance_2", 
            "active_substance_3" : "active_substance_4", 
            "active_substance_4" : "active_substance_4", 
            "mah_name" : "this is mah name", 
            "__v" : NumberInt(0)
        }, 
        "qty" : NumberInt(17)
    }
], 
"acceptance_time" : ISODate("2018-06-20T13:29:37.778+0000"), 
"created_at" : ISODate("2018-06-20T13:29:37.779+0000"), 
"guest_id" : "5b0d1b2bf734e104f46e3403", 
"vendor_id" : "5b11212d4405ce1b30663152", 
"__v" : NumberInt(0)
}

I want to run an update query in node js Mongoose Module which will change "is picked" value inside product_details, my current code is here which is not working.

let reservation = await Reservation.findById(req.params.id);
            if (reservation !== null) {
                var ctr = 0;
                reservation.products_details.forEach(async function (pro_id, index_fe, array) 
                {
                    let cart_item = await Reservation.update({
                        '_id':  req.params.id,
                        'products_details.product._id': {$in: req.body.product_ids},
                    }, {$set: {'products_details.is_picked': true}});
                });
                return callback(null, utility.success_response({
                            reservation_id: req.params.id, 
                            product_ids: req.body.product_ids
                        }, "Reservation is marked picked now."));
            }

Upvotes: 0

Views: 41

Answers (1)

Khay
Khay

Reputation: 1570

You can use javascript includes() to check _id inside product_details and model.save() to update the document instead of updating document many times depends on how many items inside product_ids.

Reservation.findById(req.params.id, (err, reservation) => {
    if (reservation) {
        reservation.products_details.forEach((product) => {
            if (req.body.product_ids.includes(product.product._id)) {
                product.is_picked  = true;
            }
        })
        reservation.save((err, callback) => {});
    }
})

Upvotes: 1

Related Questions