Discussion:
[mongodb-user] Can I apply forEach in aggregate query in MongoDB?
Shaishab Roy
2016-07-21 06:53:55 UTC
Permalink
I have a member collection and find members by specific conditions and
after get members I need to do some calculation for each member. To
calculate need query on the same collection.

My process is

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]} },
sameCount: { $sum: {$cond:[{$eq:
["$numberOfInvitees",doc.numberOfInvitees]},1,0]} }
}
}
]).toArray();
});

**My question is how can I do this using single aggregate query?**

can any one help me pls :)
--
You received this message because you are subscribed to the Google Groups "mongodb-user"
group.

For other MongoDB technical support options, see: https://docs.mongodb.com/manual/support/
---
You received this message because you are subscribed to the Google Groups "mongodb-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to mongodb-user+***@googlegroups.com.
To post to this group, send email to mongodb-***@googlegroups.com.
Visit this group at https://groups.google.com/group/mongodb-user.
To view this discussion on the web visit https://groups.google.com/d/msgid/mongodb-user/afcef1b4-30d8-48c3-9166-951846c6e2ff%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
s***@gmail.com
2016-07-22 06:18:05 UTC
Permalink
You can add currentDate in _id for the group pipeline.

$group : {
_id : { month: { $month: "$date" }, day: { $dayOfMonth: "$date" }, year: { $year: "$date" }, memberType:"$memberType",

country:"$country"

}


https://docs.mongodb.com/manual/reference/operator/aggregation/group/

So now you will get summary for per day per your group in single query.
Post by Shaishab Roy
I have a member collection and find members by specific conditions and
after get members I need to do some calculation for each member. To
calculate need query on the same collection.
My process is
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"},
["$numberOfInvitees",0]},1,0]} },
["$numberOfInvitees",doc.numberOfInvitees]},1,0]} }
}
}
]).toArray();
});
**My question is how can I do this using single aggregate query?**
can any one help me pls :)
--
You received this message because you are subscribed to the Google Groups "mongodb-user"
group.

For other MongoDB technical support options, see: https://docs.mongodb.com/manual/support/
---
You received this message because you are subscribed to the Google Groups "mongodb-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to mongodb-user+***@googlegroups.com.
To post to this group, send email to mongodb-***@googlegroups.com.
Visit this group at https://groups.google.com/group/mongodb-user.
To view this discussion on the web visit https://groups.google.com/d/msgid/mongodb-user/9eac3a7f-16f7-46ca-979c-cb68d61fc2d6%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Chris Cunningham
2016-07-29 02:43:10 UTC
Permalink
Shaishab,

This question was also posted on Stackoverflow
<http://stackoverflow.com/questions/38496419/can-i-apply-foreach-in-aggregate-query-in-mongodb>
and has been answered.

Thanks

Chris
​
--
You received this message because you are subscribed to the Google Groups "mongodb-user"
group.

For other MongoDB technical support options, see: https://docs.mongodb.com/manual/support/
---
You received this message because you are subscribed to the Google Groups "mongodb-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to mongodb-user+***@googlegroups.com.
To post to this group, send email to mongodb-***@googlegroups.com.
Visit this group at https://groups.google.com/group/mongodb-user.
To view this discussion on the web visit https://groups.google.com/d/msgid/mongodb-user/5afd616b-d173-4437-8e06-b79387af80c8%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Loading...