简体   繁体   English

MongoDB-从对象数组中拉出

[英]MongoDB - Pull from array of objects

I have a collection 我有一个收藏

{
    "_id" : ObjectId("534bae30bf5049a522e502fe"),
    "data" : [
            {
                    "0" : {
                            "content" : "1",
                            "type" : "text",
                            "ident" : true
                    },
                    "1" : {
                            "content" : "",
                            "type" : "text",
                            "ident" : false
                    }

            },
            {
                    "0" : {
                            "content" : "2",
                            "type" : "text",
                            "ident" : true
                    },
                    "1" : {
                            "content" : "",
                            "type" : "text"
                    }
            }
    ]

} }

content is unique. content是独一无二的。

How would i remove the object that matches content: '2' ? 我将如何删除与content: '2'匹配的对象?

I have tried this: 我已经试过了:

data:{$pull:{"content": deletions[i]}}

where deletions [i] is the content. 其中deletions [i]是内容。

and several variations, but i can not get it to work. 和几种变体,但我无法使其正常工作。 What am I missing? 我想念什么?

As per you comment, you should be worried. 根据您的评论,您应该担心。 I have seen this a few times particularly with PHP applications ( and PHP has this funny kind of notation in dumping arrays ). 我已经多次见过这种情况,特别是在PHP应用程序中(PHP在转储数组中有这种有趣的表示法)。

The problem is the elements such as the "0" and "1" create sub-documents in themselves, and as opposed to just leaving everything under that structure as the sub-document in itself as the the array member then you run into a problem with accessing individual members of the array as the paths used need to be "absolute". 问题是诸如“ 0”和“ 1”之类的元素本身会创建子文档,而不是仅将该结构下的所有内容保留为子文档本身作为数组成员,然后您就会遇到问题访问数组的单个成员,因为使用的路径需要是“绝对的”。

So this actually "forces" no other possible option to access the elements by what would be the equivalent "dot notation" form. 因此,这实际上不会“迫使”其他等效的“点表示法”形式来访问元素。 Except in this case it's not just the "nth" element of the array, but the actual path you need to address. 除了在这种情况下,它不仅是数组的“第n个”元素,而且是您需要解决的实际路径。

But if this does actually work for you, and it does seem like "someone" was trying to avoid the problems with positional updates under "nested" arrays ( see the positional $ operator documentation for details ) then you update can be performed like this: 但是,如果这确实对您有用,并且似乎“某人”正试图避免“嵌套”数组下的位置更新出现问题(有关详细信息,请参见位置$运算符文档),那么可以按以下方式执行更新:

The basic statement is as follows: 基本声明如下:

db.collection.update(
    {
        "data.0.context": 2
    },
    {
        "$pull": { "data.$.0.context": 2 }
    }
)

That does "seem" to be a bit of a funny way to write this, but on investigating the actual structure you have then you should be able to see the reasons why this is needed. 这样做确实看起来有点“有趣”,但是在研究实际结构时,您应该能够知道为什么需要这样做。 Essentially this meets the requirements of using the positional $ operator to indicate the index of the first matched element in the array ( aka "data" ) and then uses the standard sub-document notation in order to specify the path to the element to be updated. 从本质上讲,这符合使用位置$运算符指示数组中第一个匹配元素的索引(也称为“数据”)的要求,然后使用标准子文档表示法来指定要更新的元素的路径的要求。

So of course this poses a problem if the element is actually in an unknown position. 因此,如果元素实际上处于未知位置,这当然会带来问题。 But the thing to consider is which usage of the array is actually important to you given the documented limitation? 但是要考虑的是,鉴于记录的限制,数组的哪种用法实际上对您来说很重要? If yo need to match the position of the "inner" element, then change the structure to put the arrays in there. 如果您需要匹配“内部”元素的位置,请更改结构以将数组放在其中。

But always understand the effects of the limitation, and try to model according to what the engine can actually do. 但是,请务必了解限制的影响,并尝试根据引擎的实际功能进行建模。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM