MongoDB 使用或查询更新多个子文档

编程入门 行业动态 更新时间:2024-10-23 10:19:23
本文介绍了MongoDB 使用或查询更新多个子文档的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我想一次对多个子文档进行更新查询.我会将所有子文档状态设置为被动,其中它们满足我在查询时给出的条件.

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 使用或查询更新多个子文档

    本文发布于:2023-11-22 18:33:51,感谢您对本站的认可!
    本文链接:https://www.elefans.com/category/jswz/34/1618533.html
    版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
    本文标签:多个   文档   MongoDB

    发布评论

    评论列表 (有 0 条评论)
    草根站长

    >www.elefans.com

    编程频道|电子爱好者 - 技术资讯及电子产品介绍!