我有一个成员集合,并按特定条件查找成员,在获得成员后,我需要为每个成员进行一些计算。计算需要查询同一集合。
我的流程是
var eachMemberInfo = [];
var members = db.collection('member').find({ createdDate: currentDate, country: 'BD'}).toArray();
members.forEach(function(doc) {
var result = db.collection('member').aggregate([
{ $match: { memberType: doc.memberType, country : doc.country } },
{
$group: {
_id: {memberType:"$memberType",country:"$country"},
memberCount: { $sum: {$cond:[{$gt: ["$numberOfInvitees",0]},1,0]} },
lessCount: { $sum: {$cond:[{$and:[{$lt:["$numberOfInvitees", doc.numberOfInvitees]}, {$gt: ["$numberOfInvitees",0]}]},1,0]} },
sameCount: { $sum: {$cond:[{$eq: ["$numberOfInvitees",doc.numberOfInvitees]},1,0]} }
}
}
]).toArray();
eachMemberInfo.push({memberId:doc.memberId,memberCount: result[0].memberCount, lessCount: result[0].lessCount});
});
我的问题是如何使用单一聚合查询来完成此操作?
任何人都可以帮助我:)
例如:
成员集合如:
[{
"_id" : ObjectId("57905b2ca644ec06142a8c06"),
"memberID" : 80,
"memberType" : "N",
"numberOfInvitees" : 2,
"createdDate" : ISODate("2016-07-21T00:00:00.000Z"),
"country" : "BD"
},
{
"_id" : ObjectId("57905b2ca644ec06142a8c09"),
"memberID" : 81,
"memberType" : "N",
"numberOfInvitees" : 3,
"createdDate" : ISODate("2016-07-21T00:00:00.000Z"),
"country" : "BD"
}
{
"_id" : ObjectId("57905b2ca644ec06142a8fgh"),
"memberID" : 82,
"memberType" : "N",
"numberOfInvitees" : 4,
"createdDate" : ISODate("2016-07-21T00:00:00.000Z"),
"country" : "BD"
}
{
"_id" : ObjectId("57905b2ca644ec06142a8cfgd"),
"memberID" : 83,
"memberType" : "N",
"numberOfInvitees" : 1,
"createdDate" : ISODate("2016-07-21T00:00:00.000Z"),
"country" : "BD"
}
{
"_id" : ObjectId("57905b2ca644ec06142a8cfgd"),
"memberID" : 84,
"memberType" : "N",
"numberOfInvitees" : 2,
"createdDate" : ISODate("2016-07-21T00:00:00.000Z"),
"country" : "BD"
}
..............
]
eachMemberInfo 中的预期结果,如:
[
{ memberID : 80, memberCount:5,lessCount: 1,sameCount:2 },
{ memberID : 81, memberCount:5,lessCount: 3,sameCount:1 },
{ memberID : 82, memberCount:5,lessCount: 4,sameCount:1 },
{ memberID : 83, memberCount:5,lessCount: 0,sameCount:1 },
{ memberID : 84, memberCount:5,lessCount: 1,sameCount:2 }
]
答案 0 :(得分:6)
您无法通过聚合管道执行此操作。您应该了解MongoDB聚合是应用于集合的一系列特殊运算符。当您执行聚合管道时,MongoDB将运算符彼此管道连接,即运算符的输出成为以下运算符的输入。每个运算符的结果是一个新的文档集合。
因此,您在上面尝试实现的内容可以简单地重写为以下管道,而无需首先创建文档数组:
var collection = db.collection('member'),
pipeline = [
{ "$match": { createdDate: currentDate, country: 'BD' } },
{
"$group": {
"_id": { "memberType": "$memberType", "country": "$country" },
"memberCount": {
"$sum": { "$cond":[ { "$gt": ["$numberOfInvitees", 0] }, 1, 0 ] }
},
"sameCount": { "$sum": 1 }
}
}
];
collection.aggregate(pipeline, function(err, result){
if (err) throw err;
console.log(result);
});
<强>更新强>
对问题的更改进行跟进,运行以下聚合管道将为您提供所需的结果:
var collection = db.collection('member'),
pipeline = [
{ "$match": { createdDate: currentDate, country: 'BD' } },
{
"$group": {
"_id": {
"memberType": "$memberType",
"country": "$country"
},
"invitees":{
"$push": {
"memberID": "$memberID",
"count": "$numberOfInvitees"
}
},
"inviteesList": { "$push": "$numberOfInvitees" },
"memberCount": { "$sum": 1 }
}
},
{ "$unwind": "$invitees" },
{ "$unwind": "$inviteesList" },
{
"$group": {
"_id": "$invitees.memberID",
"sameInviteesCount": {
"$sum": {
"$cond": [
{ "$eq": ["$inviteesList", "$invitees.count"] },
1, 0
]
}
},
"lessInviteesCount": {
"$sum": {
"$cond":[
{ "$lt": ["$inviteesList", "$invitees.count"] },
1, 0
]
}
},
"memberCount": { "$first": "$memberCount" }
}
}
];
collection.aggregate(pipeline, function(err, result){
if (err) throw err;
console.log(result);
});