我有一个成员集合,并按特定条件查找成员,在获得成员后,我需要为每个成员进行一些计算。计算需要查询同一个集合。
var eachMemberInfo = [];
var members = db.collection('member').find({ createdDate: currentDate, country: 'BD'})
.skip(skipSize).limit(1000).toArray();
members.forEach(function(doc) {
/* =================calculate resultRatingCW for each member=================*/
var resultRatingCW = db.collection('member').aggregate([
{ $match: { memberType: doc.memberType, country : doc.country,createdDate: currentDate } },
{
$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]} }
}
},
{$project: { ratingCW: { $multiply: [{$divide: [{$sum: ["$lessCount", {$multiply: ["$sameCount", 0.5]}]}, "$memberCount"]}, 100]}, _id:1}}
]).toArray();
/* =================calculate resultRatingWW for each member=================*/
var resultRatingWW = db.collection('member').aggregate([
{ $match: { memberType: doc.memberType,createdDate: currentDate } },
{
$group: {
_id: {memberType:"$memberType"},
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]} }
}
},
{$project: { ratingWW: { $multiply: [{$divide: [{$sum: ["$lessCount", {$multiply: ["$sameCount", 0.5]}]}, "$memberCount"]}, 100]}, _id:1}}
]).toArray();
/* =================calculate resultIncoCW for each member=================*/
var resultIncoCW = db.collection('member').aggregate([
{ $match: { memberType: doc.memberType, country : doc.country, createdDate: currentDate } },
{
$group: {
_id: {memberType:"$memberType",country:"$country"},
memberCount: { $sum: {$cond:[{$gt: ["$memberTotalDistLocCurrency",0]},1,0]} },
lessCount: { $sum: {$cond:[{$and:[{$lt:["$memberTotalDistLocCurrency", doc.memberTotalDistLocCurrency]}, {$gt: ["$memberTotalDistLocCurrency",0]}]},1,0]} },
sameCount: { $sum: {$cond:[{$eq: ["$memberTotalDistLocCurrency",doc.memberTotalDistLocCurrency]},1,0]} }
}
},
{$project: { incoCW: { $multiply: [{$divide: [{$sum: ["$lessCount", {$multiply: ["$sameCount", 0.5]}]}, "$memberCount"]}, 100]}, _id:1}}
]).toArray();
/* =========calculate resultIncoWW for each member=================*/
var resultIncoWW = db.collection('member').aggregate([
{ $match: { memberType: doc.memberType, country : doc.country, createdDate: currentDate } },
{
$group: {
_id: {memberType:"$memberType"},
memberCount: { $sum: {$cond:[{$gt: ["$memberTotalDistUSD",0]},1,0]} },
lessCount: { $sum: {$cond:[{$and:[{$lt:["$memberTotalDistUSD", doc.memberTotalDistUSD]}, {$gt: ["$memberTotalDistUSD",0]}]},1,0]} },
sameCount: { $sum: {$cond:[{$eq: ["$memberTotalDistUSD",doc.memberTotalDistUSD]},1,0]} }
}
},
{$project: { incoWW: { $multiply: [{$divide: [{$sum: ["$lessCount", {$multiply: ["$sameCount", 0.5]}]}, "$memberCount"]}, 100]}, _id:1}}
]).toArray();
eachMemberInfo.push(
{
memberId:doc.memberId,
RatingCW: resultRatingWW[0].ratingCW,
resultRatingWW: resultRatingWW[0].ratingWW,
resultIncoCW: resultIncoCW[0].incoCW,
resultIncoWW: resultIncoWW[0].incoWW
});
});
我的问题是,是否可以使用单一聚合查询?
或任何最佳解决方案?
任何人都可以帮助我:)
例如:
成员集合如:
[{
"_id" : ObjectId("57905b2ca644ec06142a8c06"),
"memberID" : 80,
"memberType" : "N",
"numberOfInvitees" : 2,
"createdDate" : ISODate("2016-07-21T00:00:00.000Z"),
"country" : "BD",
"memberTotalDistUSD": 2,
"memberTotalDistLocCurrency": 2
},
{
"_id" : ObjectId("57905b2ca644ec06142a8c09"),
"memberID" : 81,
"memberType" : "N",
"numberOfInvitees" : 3,
"createdDate" : ISODate("2016-07-21T00:00:00.000Z"),
"country" : "BD",
"memberTotalDistUSD": 3,
"memberTotalDistLocCurrency": 3
}
{
"_id" : ObjectId("57905b2ca644ec06142a8fgh"),
"memberID" : 82,
"memberType" : "N",
"numberOfInvitees" : 4,
"createdDate" : ISODate("2016-07-21T00:00:00.000Z"),
"country" : "BD",
"memberTotalDistUSD": 4,
"memberTotalDistLocCurrency": 4
}
{
"_id" : ObjectId("57905b2ca644ec06142a8cfgd"),
"memberID" : 83,
"memberType" : "N",
"numberOfInvitees" : 1,
"createdDate" : ISODate("2016-07-21T00:00:00.000Z"),
"country" : "BD"
"memberTotalDistUSD": 1,
"memberTotalDistLocCurrency": 1
}
{
"_id" : ObjectId("57905b2ca644ec06142a8cfgd"),
"memberID" : 84,
"memberType" : "N",
"numberOfInvitees" : 2,
"createdDate" : ISODate("2016-07-21T00:00:00.000Z"),
"country" : "BD",
"memberTotalDistUSD": 2,
"memberTotalDistLocCurrency": 2
}
..............
]
eachMemberInfo 中的预期结果,如:
[
{ memberID : 80, RatingCW:4,ratingWW: 4, incoCW:4, incoWW:3},
{ memberID : 81, RatingCW:4.5,ratingWW: 4.5, incoCW:4.5, incoWW:2.5 },
{ memberID : 82, RatingCW:5,ratingWW: 5, incoCW:5, incoWW:5 },
{ memberID : 83, RatingCW:3,ratingWW: 3, incoCW:3, incoWW:3 },
{ memberID : 84, RatingCW:4.5,ratingWW: .5, incoCW:4.5, incoWW:4.5 }
]
注意:结果值为虚拟但需要此格式