mongodb中的嵌套对象更新查询

时间:2018-04-04 04:57:19

标签: mongodb nested

我的procedures集合中有对象。

我想更新5996c80fca423ce1228f7690中可用的item_id preferences.items的item_status。

我想获取所有提及item_id的记录,以便我们可以更新所有匹配项。

{ 
    "_id" : ObjectId("5996d0a1ca423ce1228f777a"), 
    "status" : "Active", 
    "procedure_name" : "ATHA", 
    "procedure_for" : "Admin", 
    "created_by" : "5940c3e8734d1d79866338cf", 
    "created_on" : ISODate("2017-10-19T18:44:22.702+0000"), 
    "speciality_id" : "5751131e3a1253845560a984", 
    "speciality" : "Orthopdics", 
    "master_template_id" : "", 
    "hospital_id" : "", 
    "surgeon_nurse_id" : "", 
    "procedure_type" : "Published", 
    "published_on" : ISODate("2017-10-19T18:44:22.702+0000"), 
    "surgical_sequence" : [

    ], 
    "preferences" : [


        {
            "category_id" : "5971fae84658f5241d8a5b70", 
            "category" : "Instruments", 
            "_id" : ObjectId("59e8f2861c999f292a837304"), 
            "items" : [

                {
                    "item_id" : "5996c80fca423ce1228f7690", 
                    "item_name" : "Battery", 
                    "item_description" : "", 
                    "image_name" : "BATTERY.png", 
                    "icon_image" : "", 
                    "side_view_image" : "", 
                    "_id" : ObjectId("59e8f2861c999f292a837306"), 
                    "attributes" : [

                    ], 
                    "subcategory" : [
                        {
                            "name" : "Power tool", 
                            "subcategory_id" : "5996c80eca423ce1228f7549", 
                            "parent_id" : "", 
                            "_id" : ObjectId("5996c80fca423ce1228f7709")
                        }, 
                        {
                            "name" : "Battery", 
                            "subcategory_id" : "5996c80eca423ce1228f750e", 
                            "parent_id" : "5996c80eca423ce1228f7549", 
                            "_id" : ObjectId("5996c80fca423ce1228f7708")
                        }
                    ]
                }
            ]
        }
    ], 
    "__v" : NumberInt(0), 
    "modified_by" : "5940c3e8734d1d79866338cf", 
    "modified_on" : ISODate("2017-10-19T18:44:22.702+0000")
}

1 个答案:

答案 0 :(得分:0)

我假设您正在讨论更新status字段,因为我找不到名称为item_status的字段。如果是这种情况,则以下查询应该有效:

> db.procedures.updateMany({"preferences.$.items.$.item_id": "5996c80fca423ce1228f7690"}, {"$set": {"status": "new_status"}})