1
{
_id: ObjectId("52ca2d45b80de42808000001"),
id: "1111139048239",
name: "Bruce Lim",
first_name: "Bruce",
last_name: "Lim",
friends: [
{
id: "1913681",
name: "John Sim",
icon: "https://fbcdn-profile-a.akamaihd.net/hprofile-ak-ash2/1117702_1913681_1171369396_q.jpg",
photos: [
{
src: "https://scontent-a.xx.fbcdn.net/hphotos-ash2/t1/230718_10150181312510976_7606555_n.jpg",
lat: "38.2289",
lng: "-85.7495"
},
{
src: "https://scontent-b.xx.fbcdn.net/hphotos-frc3/230480_10150181312620976_3864544_n.jpg",
lat: "38.2289",
lng: "-85.7495"
}
]
},
{
id: "31925743892",
name: "Mike Holloway",
icon: "https://fbcdn-profile-a.akamaihd.net/hprofile-ak-prn2/211634_31925743892_1471358831_q.jpg",
photos: [
{
src: "https://scontent-a.xx.fbcdn.net/hphotos-ash2/t1/230718_10150181312510976_7606555_n.jpg",
lat: "38.2289",
lng: "-85.7495"
},
{
src: "https://scontent-b.xx.fbcdn.net/hphotos-frc3/230480_10150181312620976_3864544_n.jpg",
lat: "38.2289",
lng: "-85.7495"
}
]
},
{
id: "1954048",
name: "Christiana Basset",
icon: "https://fbcdn-profile-a.akamaihd.net/hprofile-ak-prn2/211634_1954048_1471358831_q.jpg",
photos: [
{
src: "https://scontent-a.xx.fbcdn.net/hphotos-ash2/t1/230718_10150181312510976_7606555_n.jpg",
lat: "38.2289",
lng: "-85.7495"
},
{
src: "https://scontent-b.xx.fbcdn.net/hphotos-frc3/230480_10150181312620976_3864544_n.jpg",
lat: "38.2289",
lng: "-85.7495"
}
]
}
]
文檔}MongoDB的查詢從
選擇多個子集,當我查詢這些文檔這個集合與
db.mapping.find(
{"id":"1111139048239"},
{"friends":{
$elemMatch:{"id":"1913681"}
}}
)
我得到一個匹配friend
子回來了。
{
"_id" : ObjectId("52ca2d45b80de42808000001"),
"friends" : [
{
"id" : "1913681",
"name" : "John Sim",
"icon" : "https://fbcdn-profile-a.akamaihd.net/hprofile-ak-ash2/1117702_1913681_1171369396_q.jpg",
"photos" : [
{
"src" : "https://scontent-a.xx.fbcdn.net/hphotos-ash2/t1/230718_10150181312510976_7606555_n.jpg",
"lat" : "38.2289",
"lng" : "-85.7495"
},
{
"src" : "https://scontent-b.xx.fbcdn.net/hphotos-frc3/230480_10150181312620976_3864544_n.jpg",
"lat" : "38.2289",
"lng" : "-85.7495"
}
]
}
]
}
如何選擇多個子集。
db.mapping.find(
{"id":"1111139048239"},
{"friends":{
$elemMatch:{"id":"1913681", "id":"1954048"}
}}
)
db.mapping.find(
{"id":"1111139048239"},
{"friends":{
$elemMatch:{"id":"1913681"},
$elemMatch:{"id":"1954048"}
}}
)
只得到最後一場比賽,在這種情況下是1954048
。我如何得到兩個 - 1913681,1954048?
'$ elemMatch'只返回第一匹配元件在陣列中。要匹配多個元素,您必須使用聚合框架。有關此示例,請參閱:[只提取數組中的選定元素](http://stackoverflow.com/questions/3985214/mongodb-extract-only-the-selected-item-in-array/12241733#12241733)。 – Stennie