如何更新子文件mongodb

时间:2017-03-23 07:38:16

标签: mongodb subdocument

这是我的收藏:

{
    "_id" : ObjectId("58d32e3c8726e31b00004ac9"), 
    "buyer" : "zul@buyer1001", 
    "sellers" : [
        {
            "seller" : "razi@seller1001", 
            "items" : [
                {
                    "item_name" : "Mizuno Wave Lightning Z3", 
                    "brand" : "Mizuno",
                    "status" : "Disabled"
                },
                {
                    "item_name" : "Mizuno Wave Fang SS", 
                    "brand" : "Mizuno",
                    "status" : "Disabled"
                }
            ]
         }
    ]
}

如何将所有项目状态更新为“启用”?

1 个答案:

答案 0 :(得分:0)

对于Mongo shell,您可以使用此命令

db.collection.find({}).forEach(function(doc) {
doc.sellers.forEach(function(list) {
    list.items.forEach(function(object) {
        object.status = "Enabled";
    });
});
db.collection.save(doc);
});