Saturday, 14 September 2013

Can you fake a join in Mongo aggregation

Can you fake a join in Mongo aggregation

I have documents of the form
members:
{
_id:ObjectId("xxx"),
name:"John",
branch:ObjectId("yyy")
}
branches:
{
_id:ObjectId("yyy"),
name:"Branch A"
}
I want to report on the number of people in each branch. A straight $group
pipeline does this but I can only get it to report back the branch _id and
then convert it in my code. I would like to do something like:
db.members.aggregate(
{$group:{_id:db.branches.findOne(
{_id:"$branch"},
{name:1}).name,count:{$sum:1}}})
Which appears not to work. Is there something along those lines that does
work?

No comments:

Post a Comment