MongoDB updating fields in nested array

后端 未结 3 2092
庸人自扰
庸人自扰 2020-12-06 13:46

MongoDB updating fields in nested array

How can I set \"play\" to \"play photo\" in the photos array?

I only know its _id.

\"_id\": ObjectId(         


        
3条回答
  •  星月不相逢
    2020-12-06 14:07

    This issue has been resoved. Feature of updating fields inside nested array of objects, is available in MongoDB 3.6+ versions. Look positional operators(all and with identifier) here.

    //Update all docs in collection matching photo name "play" to "play photo"
    db.collectioname.update(
        {},
        { $set: { "albums.$[].photos.$[photo_field].name": "play photo" } },
        { arrayFilters: [  {"photo_field.name": "play"} ], multi: true}
    );
    
    //Update this specific doc given in question matching photo name "play" to "play photo"
    db.collectioname.update(
        {"_id" : ObjectId("4f41a5c7c32810e404000000")},
        { $set: { "albums.$[].photos.$[photo_field].name": "play photo" } },
        { arrayFilters: [  {"photo_field.name": "play"} ]}
    );
    

    This is for the help of people coming here after MongoDB 3.6

提交回复
热议问题