Here is an example from MongoDB tutorial (here it collection ZIP Code db:
db.zipcodes.aggregate( [
{ $group: { _id: \"$state\", totalPop: { $sum: \"$pop\"
In a $group
stage, _id
is used to designate the group condition. You obviously need it.
If you're familiar with the SQL world, think of it as the GROUP BY
clause.
Please note, in that context too, _id
is really an unique identifier in the generated collection, as by definition $group
cannot produce two documents having the same value for that field.