更新mongodb中的嵌套值

时间:2018-06-20 13:51:35

标签: node.js mongoose

我在更新product_details信息中的值时遇到问题。我发送了预订ID,以及product_details信息中的产品ID,其“ is_picked”值需要标记为“ true”。 我有一个预订表,我正在保存预订。这是它的架构

{ 
"_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)
}

我想在节点js猫鼬模块中运行一个更新查询,这将更改product_details中的“被拾取”值,我的当前代码在这里不起作用。

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."));
            }

1 个答案:

答案 0 :(得分:1)

您可以使用JavaScript includes()来检查_id内的product_detailsmodel.save()来更新文档,而不是多次更新文档,这取决于{{1 }}。

product_ids