简体   繁体   English

mongodb在Array中查找元素

[英]mongodb find elements in Array

I am playing around with mongodb and having troubles with finding elements in an array 我正在玩mongodb并在查找数组中的元素时遇到麻烦

My collection looks like this 我的收藏看起来像这样

{
        "_id" : ObjectId("4c4b1476238d3b4dd5000002"),
        "username" : "pbakkum",
        "first_name" : "Peter",
        "last_name" : "Bakkum",
        "age" : 21,
        "addresses" : [
                {
                        "name" : "home",
                        "street" : "588 5th Street",
                        "city" : "Brooklyn",
                        "state" : "CA",
                        "zip" : 11215
                },
                {
                        "name" : "work",
                        "street" : "588 5th Street",
                        "city" : "Brooklyn",
                        "state" : "CA",
                        "zip" : 11215
                },
                {

                }
        ]
}
{
        "_id" : ObjectId("4c4b1476238d3b4dd5000001"),
        "username" : "kbanker",
        "email" : "kylebanker@gmail.com",
        "first_name" : "Kyle",
        "last_name" : "Banker",
        "hashed_password" : "bd1cfa194c3a603e7186780824b04419",
        "addresses" : [
                {
                        "name" : "home",
                        "street" : "588 5th Street",
                        "city" : "Brooklyn",
                        "state" : "NY",
                        "zip" : 11215
                },
                {
                        "name" : "work",
                        "street" : "1 E. 23rd Street",
                        "city" : "New York",
                        "state" : "NY",
                        "zip" : 10010
                }
        ],
        "payment_methods" : [
                {
                        "name" : "VISA",
                        "last_four" : 2127,
                        "crypted_number" : "43f6ba1dfda6b8106dc7",
                        "expiration_date" : ISODate("2016-05-01T07:00:00Z")
                }
        ],
        "age" : 151
}

When I fire a query which is something like this. 当我触发一个像这样的查询。

db.users.find({"addresses.name":"home","addresses.state":"NY"});

the output of this query is a single record with the username as kbanker 此查询的输出是单个记录,用户名为kbanker

This implies that it is exactly matching both the parameters "name":"home and addresses.state:NY and returning only one record. 这意味着它完全匹配参数“name”:“home和addresses.state:NY并且只返回一条记录。

However as per my understanding, this should return both the records since it should check for any of the parameters "name":"home" and addressess.state:NY in the enitre array and if any of them matches it should display that record. 但是根据我的理解,这应该返回两个记录,因为它应该检查任何参数“name”:“home”和addressess.state:enitre数组中的NY,如果它们中的任何一个匹配,它应该显示该记录。

This behavior of selecting just a single record defeats the purpose of $elemMatch attribute ? 选择单个记录的这种行为违背了$ elemMatch属性的目的?

Am I correct or something has changed in the new version of mongodb. 我是正确的还是新版本的mongodb中有些变化。 I am using 3.2.5 我使用的是3.2.5

Let me try to explain using your data set 让我试着解释一下你的数据集

db.foo.insert({
        "username" : "pbakkum",
        "addresses" : [
                {
                        "name" : "home",
                        "state" : "CA"
                },
                {
                        "name" : "work",
                        "state" : "CA"
                }
        ]
});
db.foo.insert({
        "username" : "kbanker",
        "addresses" : [
                {
                        "name" : "home",
                        "state" : "NY"
                },
                {
                        "name" : "work",
                        "state" : "NY"
                }
        ]
});

When you execute the query db.foo.find({"addresses.name":"home","addresses.state":"NY"}); 执行查询时db.foo.find({"addresses.name":"home","addresses.state":"NY"}); mongo search in the array for documents that have "home" and "NY" it does not matter if the values are in the same document or not. mongo在数组中搜索具有“home”和“NY”的文档,如果值在同一文档中,则无关紧要。

> db.foo.find({"addresses.name":"home","addresses.state":"NY"}, { "_id":0, "user
name" :1, "addresses" :1 })
{ "username" : "kbanker", "addresses" : [ { "name" : "home", "state" : "NY" }, {
 "name" : "work", "state" : "NY" } ] }

As you can see it will be enough to have "home" and "NY" in one of the array documents, the difference with $elemMatch is that both of them ("home" and "NY") need to be in the same document. 正如你所看到的,在一个数组文档中有“home”和“NY”就足够了,与$ elemMatch的区别在于它们(“home”和“NY”)都需要在同一个文档中。

Lets add another document in the array of the username pbakkum. 让我们在用户名pbakkum的数组中添加另一个文档。

db.foo.insert({
        "username" : "pbakkum",
        "addresses" : [
                {
                        "name" : "home",
                        "state" : "CA"
                },
                {
                        "name" : "work",
                        "state" : "CA"
                },
                {
                        "name" : "work",
                        "state" : "NY"  // Adding NY to make the match of your query
                }
        ]
});

Running the same query, this time you will see both records as both of them in the array contain "home" and "NY" in one of the documents 运行相同的查询,这次您将看到两个记录,因为数组中的两个记录都包含其中一个文档中的“home”和“NY”

> db.foo.find({"addresses.name":"home","addresses.state":"NY"}, { "_id":0, "user
name" :1, "addresses" :1 })
{ "username" : "pbakkum", "addresses" : [ { "name" : "home", "state" : "CA" }, {
 "name" : "work", "state" : "CA" }, { "name" : "work", "state" : "NY" } ] }
{ "username" : "kbanker", "addresses" : [ { "name" : "home", "state" : "NY" }, {
 "name" : "work", "state" : "NY" } ] }

$elemMatch get only the document that have the perfect match within the same document ("home" and "NY") $elemMatch只获取同一文档中完美匹配的文档(“home”和“NY”)

> db.foo.find( { addresses : {$elemMatch : {"name":"home","state":"NY"}}}, { "_i
d":0, "username" :1, "addresses" :1 })
{ "username" : "kbanker", "addresses" : [ { "name" : "home", "state" : "NY" }, {
 "name" : "work", "state" : "NY" } ] }

This implies that it is exactly matching both the parameters "name":"home and addresses.state:NY and returning only one record. 这意味着它完全匹配参数“name”:“home和addresses.state:NY并且只返回一条记录。

Yes query is matching both parameters but not interesting if they are in the same document, this is just a coincidence. 是查询匹配两个参数但如果它们在同一文档中则不感兴趣,这只是巧合。

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

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