2013-10-27 102 views
1

在集合「友誼」中,我想找到Bart的所有朋友都是10. $ elemMatch查詢只返回數組的第一個匹配元素:我只得到Milhouse。我怎樣才能得到米爾豪斯和馬丁?如何從Mongodb的相同數組中找到多個項目?

{ name:'Bart', 
    age:10 
    friends:[ 
     { name:'Milhouse', 
      age:10 
     }, 
     { name:'Nelson', 
      age:11 
     }, 
     { name:'Martin', 
      age:10 
     } 
    ] 
}, 
{ name:'Lisa', 
    age:8 
    friends:[ 
     ... 
    ] 
} 
+0

您是否嘗試過自己先有任何疑問?你可以請他們發佈? –

回答

1

嘗試使用aggregation framework這個任務(尤其是$unwind運營商):

db.friendship.aggregate([ 
    { $match: { name: "Bart" } }, 
    { $unwind: "$friends" }, 
    { $match: { "friends.age": 10 } } 
]); 
相關問題