mongodb在Array中查找元素

时间:2016-05-05 21:30:07

标签: arrays mongodb mongodb-query

我正在玩mongodb并且在查找数组中的元素时遇到麻烦

我的收藏品看起来像这样

{
        "_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
}

当我触发一个类似这样的查询时。

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

此查询的输出是单个记录,用户名为kbanker

这意味着它完全匹配参数" name":" home和addresses.state:NY并且只返回一条记录。

但是根据我的理解,这应该返回两个记录,因为它应该检查任何参数" name":" home"和addressess.state:enitre数组中的NY,如果它们中的任何一个匹配,它应该显示该记录。

这种只选择一条记录的行为会破坏$ elemMatch属性的目的吗?

我是否正确或在新版本的mongodb中有所改变。 我使用的是3.2.5

1 个答案:

答案 0 :(得分:2)

让我试着用你的数据集来解释

requestLayout()

当您在数组中为具有“home”和“NY”的文档执行查询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" } ] }); mongo搜索时,这些值是否在同一文档中无关紧要。

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

正如你所看到的,在其中一个数组文档中有“home”和“NY”就足够了,与$ elemMatch的区别在于它们(“home”和“NY”)都需要在同一份文件。

让我们在用户名pbakkum的数组中添加另一个文档。

> 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" } ] }

运行相同的查询,这次您将看到两个记录,因为数组中的两个记录都包含其中一个文档中的“home”和“NY”

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
                }
        ]
});

> 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" } ] } 只获取同一文档中完美匹配的文档(“home”和“NY”)

$elemMatch
  

这意味着它完全匹配两个参数   “name”:“home and addresses.state:NY,只返回一条记录。

是查询匹配两个参数但如果它们在同一文档中则不感兴趣,这只是巧合。