Mongo implement the grouping function group

 For example, a user query the collection, female gender, count the number of the same age



db.User.group({
     key: { 
         'Age' : 1
     },
     cond: {"sex":1},
     reduce: function ( curr, result ) {
             result.total += 1;
         },
     initial: {
         total : 0 
         }
   }
   )


Equivalent to the sql
select COUNT(0) as total,Age from User where Sex = 1 GROUP BY Age 

The simple example more information view the document

https://docs.mongodb.com/manual/reference/method/db.collection.group/#db.collection.group

Published 27 original articles · won praise 53 · views 160 000 +

Guess you like

Origin blog.csdn.net/auspi12341/article/details/77196235