MongoDB - objects? Why do I need _id in aggregate

后端 未结 3 1226
悲&欢浪女
悲&欢浪女 2021-01-02 03:47

Here is an example from MongoDB tutorial (here it collection ZIP Code db:

db.zipcodes.aggregate( [
   { $group: { _id: \"$state\", totalPop: { $sum: \"$pop\"         


        
3条回答
  •  既然无缘
    2021-01-02 03:59

    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.

提交回复
热议问题