使用$ pull和$ [identifier]从嵌套数组中删除对象(mongoDB 3.6) [英] Remove an object from a nested array with $pull and $[identifier] (mongoDB 3.6)

查看:186
本文介绍了使用$ pull和$ [identifier]从嵌套数组中删除对象(mongoDB 3.6)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

MongoDB 3.6允许对任何数组中的所有匹配元素执行复杂的数组操作-无论嵌套深度如何-

MongoDB 3.6 allow to perform complex array manipulations against all matching elements in any array – no matter how deeply nested – Update Nested Arrays with $[identifier]

请考虑以下survey集合上的文档:

Consider the document below on survey collection:

{
    "_id": "5a7d86d8fac139e71b0b9f5b",
    "results": [
      {
        "items": [
          {
            "comments": [
              {
                "id" : "123456",
                "email": "user@email.com",
                "comment": "comment 1"
              }
            ]
          }
        ]
      }
    ]
  }

我正在尝试使用$pull和新的$[<identifier>]

I'm trying to remove a comment based on comment id using $pull and the new $[<identifier>]

我根据更新命令在下面尝试了command :

I tried the command below based on The update command :

db.runCommand({
  update: "survey",
  updates: [
    {
      q: {},
      u: {
        $pull: {
          "results.$[].items.$[].comments.$[comment]": { "comment.id": { $eq: "123456" } }
        }
      },
      arrayFilters: [{ "comment.id": { $eq: "123456" } }]
    }
  ]
})

但是它不起作用,因为$pull期望一个数组值: Cannot apply $pull to a non-array value

But it didn't work because $pull expect an array value: Cannot apply $pull to a non-array value

有什么帮助吗?谢谢.

推荐答案

尝试

Try positional all $[] variant.

类似

db.runCommand({
  update: "survey",
  updates: [
    {
      q: {},
      u: {
        $pull: {
          "results.$[].items.$[].comments": { "id":  "123456" }
        }
      }
    }
  ]
})

这篇关于使用$ pull和$ [identifier]从嵌套数组中删除对象(mongoDB 3.6)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆