我想一次对多个子文档进行更新查询.在满足我查询条件的情况下,我将所有子文档状态设置为被动.
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.
db.deduplications.update( { $and: [ { "_id": "189546D623FC69E3B693FDB679DBC76C" }, { "DeviceVersionPairs.DeviceId": ObjectId("5822d0606bfdcd6ec407d9b9") }, { "DeviceVersionPairs.CloudFolderId": ObjectId("5823110e6bfdd46ec4357582") }, { "DeviceVersionPairs.CloudFileId": ObjectId("582311168cd396223499942a") }, { "DeviceVersionPairs.VersionId": ObjectId("582311168cd396223499942b") } ] }, { $set: { "DeviceVersionPairs.$.Status": "passive" }});上面的查询恰好找到一个子文档,然后根据需要进行更新.但是;
Above query finds exactly one subdocument, then it makes the update as I want it. But;
db.deduplications.update( { $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.$.Status": "passive" }});当我用or填充查询段并添加其他项时,它会显示错误:
When I populate the query segment with an or, and add the other items it gives error:
WriteResult({ "nMatched" : 0, "nUpserted" : 0, "nModified" : 0, "writeError" : { "code" : 16837, "errmsg" : "The positional operator did not find the match needed from the query. Unexpanded update: DeviceVersionPairs.$.Status" } })我在这里想念什么?
推荐答案您收到此错误
位置运算符未从查询中找到所需的匹配项
The positional operator did not find the match needed from the query
第二个查询与多个子文档相匹配.当前无法使用位置运算符来使用位置运算符更新数组中的所有项目.
because of your second query matched with multiple sub-document. Currently it is not possible to use the positional operator to update all items in an array using positional operator.
因此,要解决您的问题,您可以按照以下过程进行操作
so to solve your problem you can follow this process
使用_id查找文档,并使用 $查找文档elemMatch 然后
更新每个子文档并再次保存文档
update each sub document and save document again
可以这样尝试:
db.deduplications.find( { $or: [ { "_id": ObjectId("583fc558668bde730a460e11") , "DeviceVersionPairs":{ $elemMatch:{ "DeviceId": ObjectId("5822d0606bfdcd6ec407d9b9") , "CloudFolderId": ObjectId("5823110e6bfdd46ec4357582") , "DeviceVersionPairs.CloudFileId": ObjectId("582311168cd396223499942a") , "DeviceVersionPairs.VersionId": ObjectId("582311168cd396223499942b") }} } , { "_id": ObjectId("583fc558668bde730a460e11") , "DeviceVersionPairs":{ $elemMatch:{ "DeviceId": ObjectId("56dfe1356caaea14a819f1e4") , "CloudFolderId": ObjectId("583fb4bc6e7f341874f13bfc") , "CloudFileId": ObjectId("583fb539e015b8a53fb71872") , "VersionId": ObjectId("583fb4ca6e7f331874213584") }} } ] }).forEach(function (doc) { doc.DeviceVersionPairs.forEach(function (device) { device.status = 'passive'; }); db.deduplications.save(doc); });更多推荐
MongoDB使用或查询更新多个子文档
发布评论