我玩弄的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
感謝Koitoer ..我現在明白了。 – user641887