2016-05-05 80 views
0

我玩弄的MongoDB,並具有發現元素的煩惱數組中的元素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" : "[email protected]", 
     "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一個記錄

這意味着它是精確匹配兩個參數「名」:「家庭和addresses.state:NY,只有返回一個記錄

但據我的理解,這應該返回兩個記錄,因爲它應該檢查任何參數「name」:「home」和addressess.state:NY在enitre數組中,如果其中任何一個匹配它應顯示該記錄。

只選擇單個記錄的這種行爲會損失$ elemMatch屬性的目的嗎?

我是否正確或在新版本的mongodb中發生了變化。 我使用3.2.5

回答

2

讓我嘗試使用您的數據來解釋設置

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

當你執行數組中的查詢db.foo.find({"addresses.name":"home","addresses.state":"NY"});蒙戈查找文件有「家」和「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" } ] } 

正如你可以看到這將是足有數組文件的一個「家」和「NY」,以$ elemMatch不同的是,他們兩個(「家」和「NY」)的需要在同一個文件中。

讓我們在用戶名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 
       } 
     ] 
}); 

運行相同的查詢,這時候你會看到兩個記錄既是他們的陣列中包含的文件之一「家」和「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只有得到具有相同的文檔中的完美匹配(「家」和「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" } ] } 

這意味着它是精確匹配參數 「name」:「home和addresses.state:NY並且只返回一條記錄。

是的查詢是匹配這兩個參數,但如果它們在同一個文檔中沒有興趣,這只是一個巧合。

+0

感謝Koitoer ..我現在明白了。 – user641887