简体   繁体   English

mongodb,如何在数组中查找其他数组

[英]mongodb, how to find in array other array

I need some help. 我需要协助。 I am trying to find an array in another array. 我试图在另一个数组中找到一个数组。

Records: 记录:

{
    "_id" : ObjectId("56b7e6966213e8d142ef55b7"),
    "users" : [ 
        ObjectId("56b0e547a838b5a45b4d0100"), // 0
        ObjectId("56a7e16e37e5adf32b97cc3d")  // 1
    ]
},
{
    "_id" : ObjectId("56b7e6966213e8d142ef55b7"),
    "users" : [ 
        ObjectId("56b0e547a838b5a45b4d0100"), 
        ObjectId("56a7e16e37e5adf32b97cc3d"),
        ObjectId("56b7e6b96213e8d142ef55b8")
    ]
}

I'm trying to find only first record "_id" : ObjectId("56b7e6966213e8d142ef55b7") , 我正在尝试仅查找第一个记录"_id" : ObjectId("56b7e6966213e8d142ef55b7")

I'm using query: 我正在使用查询:

db.collection.find({"users" : { $in: [ 
    ObjectId("56b0e547a838b5a45b4d0100"), 
    ObjectId("56a7e16e37e5adf32b97cc3d")
]}})

or 要么

db.collection.find({ $and: [{
    "users": ObjectId("56b0e547a838b5a45b4d0100")
}, {
    "users": ObjectId("56a7e16e37e5adf32b97cc3d")
}]})

Response gives me two records. 回复给了我两条记录。

This request gives me the correct response: 这个要求给了我正确的答案:

db.collection.find({"users" : [ 
    ObjectId("56b0e547a838b5a45b4d0100"), // 0
    ObjectId("56a7e16e37e5adf32b97cc3d")  // 1
]})

but if record has an array like this: 但如果记录具有这样的数组:

{
    "_id" : ObjectId("56b7e6966213e8d142ef55b7"),
    "users" : [ 
        ObjectId("56a7e16e37e5adf32b97cc3d"), // 1
        ObjectId("56b0e547a838b5a45b4d0100")  // 0
    ]
}

it doesn't work 它不起作用

$all, $eq, $in also doesn't work $ all,$ eq,$ in也无效

db.rooms.find({  
    users: {
        $all: [ 
        ObjectId("56a7e16e37e5adf32b97cc3d"), 
        ObjectId("56b0e547a838b5a45b4d0100")
        ]
    } 
})

I need to find rows where [1,2] == [1,2] or [1,2] == [2,1] 我需要找到[1,2] == [1,2]或[1,2] == [2,1]的行

not where [1,2,3] == [1,2] 不是[1,2,3] == [1,2]

I will appreciate if somebody can help me. 如果有人可以帮助我,我将不胜感激。

If all you expect is the resulting document with "only" those two array entries, then the combination of conditions you want is $all and $size : 如果您所期望的只是带有“仅”这两个数组条目的结果文档,则所需条件的组合为$all$size

db.rooms.find({
    "users": {
        "$all": [
                ObjectId("56b0e547a838b5a45b4d0100"),
                ObjectId("56a7e16e37e5adf32b97cc3d") 
        ],
        "$size": 2
    }
})

Where $all is basically an $and with shorter syntax, and the $size retricts that since the list as n entries, then you only want to match something of that particular length/size. 其中$all基本上是一个$and并且语法较短,而$size限制该列表作为n个条目,因此您只想匹配该特定长度/大小的内容。

This is better than specifying the array directly, since it will match in either order, as opposed to this statement that would not match considering the order is different and therefore not an "exact" match: 这比直接指定数组要好,因为它会以任意顺序匹配,而不是考虑到顺序不同而不是“精确”匹配的以下语句:

db.rooms.find({
    "users": [
        ObjectId("56a7e16e37e5adf32b97cc3d"),
        ObjectId("56b0e547a838b5a45b4d0100")
    ]
})

So logically the array contains "all" the specified entries, and is the same "size" as the input and no different. 因此,从逻辑上讲,数组包含“所有”指定的条目,并且与输入的“大小”相同且没有区别。

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

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