0
我有类似这样的一个模型:与find MongoDB的总拥有
{
email: String,
name: String,
role: String,
location: {
country: String,
city: String
},
contacts: {
email: String,
phone: String
}
}
我需要在我的视图来显示整个用户的信息,但我想包括也有多少用户从一个国家也有。
使用aggregate
我不知道如何让全部用户通过我创建的组。
因此,在我在做什么的那一刻是这样的:
User.find({}, function(err, users) {
User.aggregate([
{ $group: { _id: { country: '$location.country' }, count: { $sum: 1 }}}
], function(err, results) {
res.render('home', {
users: users,
countries: results
});
});
});
正如你可以看到我使用的查找,然后汇总两全我需要的信息......但我敢确定有一种方法可以只使用aggregate
但我找不到如何做到这一点...
如果我得到你的权利,你想限制数据返回,你可能想看看[$ project](http://docs.mongodb.org/manual/reference/operator/aggregation/project/)。 –