2013-10-27 94 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 } } 
]); 
相关问题