如何从 MongoDB 文档中的双重嵌套数组中删除元素. [英] How to remove an element from a doubly-nested array in a MongoDB document.

查看:50
本文介绍了如何从 MongoDB 文档中的双重嵌套数组中删除元素.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个类似以下内容的文档结构:

I have a document structure something along the lines of the following:

{
"_id" : "777",
"someKey" : "someValue",
"someArray" : [
    {
        "name" : "name1",
        "someNestedArray" : [
            {
                "name" : "value"
            },
            {
                "name" : "delete me"
            }
        ]
    }
  ]
}

我想删除值为delete me"的嵌套数组元素.

I want to delete the nested array element with the value "delete me".

我知道我可以使用嵌套的 $elemMatch 表达式找到与此描述匹配的文档.删除相关元素的查询语法是什么?

I know I can find documents which match this description using nested $elemMatch expressions. What is the query syntax for removing the element in question?

推荐答案

要删除有问题的项目,您实际上要使用更新.更具体地说,您将使用 $pull 命令进行更新,该命令将从数组中删除项目.

To delete the item in question you're actually going to use an update. More specifically you're going to do an update with the $pull command which will remove the item from the array.

db.temp.update(
  { _id : "777" },
  {$pull : {"someArray.0.someNestedArray" : {"name":"delete me"}}}
)

这里发生了一些魔法".使用 .0 表示我们知道我们正在修改 someArray 的第 0 项.使用 {"name":"delete me"} 表示我们知道我们计划删除的确切数据.

There's a little bit of "magic" happening here. Using .0 indicates that we know that we are modifying the 0th item of someArray. Using {"name":"delete me"} indicates that we know the exact data that we plan to remove.

如果您将数据加载到客户端然后执行更新,此过程就可以正常工作.如果您想执行执行这些操作的通用"查询,则此过程的效果较差.

This process works just fine if you load the data into a client and then perform the update. This process works less well if you want to do "generic" queries that perform these operations.

我认为最简单的认识是,更新子文档数组通常需要您在某个时候将原始文档保存在内存中.

I think it's easiest to simply recognize that updating arrays of sub-documents generally requires that you have the original in memory at some point.

针对下面的第一条评论,您可能可以通过稍微更改数据结构来帮助您的情况

In response to the first comment below, you can probably help your situation by changing the data structure a little

"someObjects" : {
  "name1":  {
        "someNestedArray" : [
            {
                "name" : "value"
            },
            {
                "name" : "delete me"
            }
        ]
    }
}

现在你可以做 {$pull : { "someObjects.name1.someNestedArray" : ...

这是你的结构的问题.MongoDB 对操作子数组"没有很好的支持.您的结构有一个对象数组,而这些对象包含更多对象的数组.

Here's the problem with your structure. MongoDB does not have very good support for manipulating "sub-arrays". Your structure has an array of objects and those objects contain arrays of more objects.

如果你有以下结构,你将很难使用像$pull这样的东西:

If you have the following structure, you are going to have a difficult time using things like $pull:

array [
  { subarray : array [] },
  { subarray : array [] },
]

如果你的结构看起来像这样并且你想更新subarray,你有两个选择:

If your structure looks like that and you want to update subarray you have two options:

  1. 更改您的结构,以便您可以利用 $pull.
  2. 不要使用$pull.将整个对象加载到客户端并使用 findAndModify.
  1. Change your structure so that you can leverage $pull.
  2. Don't use $pull. Load the entire object into a client and use findAndModify.

这篇关于如何从 MongoDB 文档中的双重嵌套数组中删除元素.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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