MongoDB update multiple subdocuments with or query

前端 未结 5 866
时光取名叫无心
时光取名叫无心 2021-01-27 14:50

I want to make an update query for multiple subdocuments at once. I will be setting all the subdocuments statuses to passive where they satisfy the condition I give on query. <

5条回答
  •  攒了一身酷
    2021-01-27 15:49

    please see whether array filters will help

    db.deduplications.updateMany(
    { $or: [ { $and: [ 
            { "_id": "189546D623FC69E3B693FDB679DBC76C" }, 
            { "DeviceVersionPairs.DeviceId": ObjectId("5822d0606bfdcd6ec407d9b9") }, 
            { "DeviceVersionPairs.CloudFolderId": ObjectId("5823110e6bfdd46ec4357582") },
            { "DeviceVersionPairs.CloudFileId": ObjectId("582311168cd396223499942a") },
            { "DeviceVersionPairs.VersionId": ObjectId("582311168cd396223499942b") }
        ] } ,   
        { $and: [ 
            { "_id": "189546D623FC69E3B693FDB679DBC76C" }, 
            { "DeviceVersionPairs.DeviceId": ObjectId("56dfe1356caaea14a819f1e4") }, 
            { "DeviceVersionPairs.CloudFolderId": ObjectId("583fb4bc6e7f341874f13bfc") }, 
            { "DeviceVersionPairs.CloudFileId": ObjectId("583fb539e015b8a53fb71872") }, 
            { "DeviceVersionPairs.VersionId": ObjectId("583fb4ca6e7f331874213584") }
        ] } ] },
    
    
         { $set: {
             "DeviceVersionPairs.index.Status": "passive" ,
           }
         },
        { 
        arrayFilters: [ 
                { $or: [ { $and: [ 
            { "index.DeviceId": ObjectId("5822d0606bfdcd6ec407d9b9") }, 
            { "index.CloudFolderId": ObjectId("5823110e6bfdd46ec4357582") },
            { "index.CloudFileId": ObjectId("582311168cd396223499942a") },
            { "index.VersionId": ObjectId("582311168cd396223499942b") }
        ] } ,   
        { $and: [ 
            { "index.DeviceId": ObjectId("56dfe1356caaea14a819f1e4") }, 
            { "index.CloudFolderId": ObjectId("583fb4bc6e7f341874f13bfc") }, 
            { "index.CloudFileId": ObjectId("583fb539e015b8a53fb71872") }, 
            { "index.VersionId": ObjectId("583fb4ca6e7f331874213584") }
        ] } ] }
              ]
        ,"multi":true, new:true
        },
    
        function (err, out) {
             //
        });
    

提交回复
热议问题