What does _id mean in Mongo $group operator

Viewed 206

I have a simple query in MongoDB as:

{ $group : { _id : { color: "$color", transport: "$transport"} , count : { $sum : 1 } } }

It means query in sql as:

select color, transport,count(1) from t group by color, transport

I would ask what _id means in the above mongodb query, if i change _id to any other string,such as groupBy, the error is: groupBy is not an aggregation operator,looks it has to be _id and can't be changed

1 Answers

Aggregation-framework in mongodb has some stages and operators and these operators have some expressions which can only work inside these stages.

Here _id is the expression which signifies the term with which you want to group with (group by color). And such like that $sum is the accumulator for the $group stage to count the number of documents which gets associated with that $grouped term.

Related