我想一次对多个子文档进行更新查询.我会将所有子文档状态设置为被动,其中它们满足我在查询时给出的条件.
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 查找文档并使用 $ 查找子文档电子匹配然后
更新每个子文档并再次保存文档
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 使用或查询更新多个子文档
发布评论