Marc Tinkler
2013-02-28 14:37:46 UTC
Hi all, we're trying to use the aggregation framework to do what (to my
mind) should be a very simple operation: keep track the people who got a
set of questions right and wrong.
Given the input documents:
{ question: 1, user: 1, correct: true },
{ question: 1, user: 2, correct: false },
{ question: 2, user: 1, correct: false },
{ question: 2, user: 2, correct: false }
We'd like to produce the following output:
{ question: 1, rightusers: [ 1 ], wrongusers: [ 2 ] }
{ question: 2, rightusers: [ ], wrongusers: [ 1, 2 ] }
The best we can seem to do is this:
aggregate(
{ $group: { _id: '$question',
rightusers: { $addToSet: { $cond : [ '$correct', '$user', null
] } },
wrongusers: { $addToSet: { $cond : [ '$correct', null, '$user'
] } }
} },
{ $project : { _id: 0, 'question': '$_id', rightusers: 1, wrongusers: 1 } }
)
which produces:
{ "rightusers" : [ null ], "wrongusers" : [ 2, 1 ],"question" : 2 },
{ "rightusers" : [ null, 1 ], "wrongusers" : [ 2, null ], "question" : 1
}
Anyone have any idea how we can eliminate the nulls?
Any help would be greatly appreciated!
Thanks,
Marc
--
--
You received this message because you are subscribed to the Google
Groups "mongodb-user" group.
To post to this group, send email to mongodb-user-/***@public.gmane.org
To unsubscribe from this group, send email to
mongodb-user+unsubscribe-/***@public.gmane.org
See also the IRC channel -- freenode.net#mongodb
---
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+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/***@public.gmane.org
For more options, visit https://groups.google.com/groups/opt_out.
mind) should be a very simple operation: keep track the people who got a
set of questions right and wrong.
Given the input documents:
{ question: 1, user: 1, correct: true },
{ question: 1, user: 2, correct: false },
{ question: 2, user: 1, correct: false },
{ question: 2, user: 2, correct: false }
We'd like to produce the following output:
{ question: 1, rightusers: [ 1 ], wrongusers: [ 2 ] }
{ question: 2, rightusers: [ ], wrongusers: [ 1, 2 ] }
The best we can seem to do is this:
aggregate(
{ $group: { _id: '$question',
rightusers: { $addToSet: { $cond : [ '$correct', '$user', null
] } },
wrongusers: { $addToSet: { $cond : [ '$correct', null, '$user'
] } }
} },
{ $project : { _id: 0, 'question': '$_id', rightusers: 1, wrongusers: 1 } }
)
which produces:
{ "rightusers" : [ null ], "wrongusers" : [ 2, 1 ],"question" : 2 },
{ "rightusers" : [ null, 1 ], "wrongusers" : [ 2, null ], "question" : 1
}
Anyone have any idea how we can eliminate the nulls?
Any help would be greatly appreciated!
Thanks,
Marc
--
--
You received this message because you are subscribed to the Google
Groups "mongodb-user" group.
To post to this group, send email to mongodb-user-/***@public.gmane.org
To unsubscribe from this group, send email to
mongodb-user+unsubscribe-/***@public.gmane.org
See also the IRC channel -- freenode.net#mongodb
---
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+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/***@public.gmane.org
For more options, visit https://groups.google.com/groups/opt_out.