The grouping aggregation in MongoDB is $group, it cannot be sort, using the following format:
{$group: {_id: <expression>, <field1>: {<accumulator1>: <expression1>}, ...}}
Where the _id attribute is required, the purpose is to specify the field or basis of the grouping, field1 for the custom field, accumulator for the accumulator, the following to count the number of user registrations per day as a column
Db.user.aggregate ([ { $group: { _id:{ year:{$year: ' $time '},//time for the registered time $year represents the year of acquisition month:{$ Month: "$time"},//$month get the month day:{$dayOfMonth: "$time"}//$dayOfMonth get the number } count:{$sum: 1}//$sum is cumulative, 1 = cumulative number } }])
If you only want to register the area for Sichuan, then add a $match before $goup:
{ $match: {location : ' Sichuan '} },
The Java implementation of the above two aggregation operations:
DBObject Filtercond =NewBasicdbobject (); Filtercond.put ("Location", "Sichuan");D bobject match=NewBasicdbobject ("$match", Filtercond);D bobject Group=Newbasicdbobject ();D bobject groupdate=NewBasicdbobject (); Groupdate.put ("Year",NewBasicdbobject ("$year", "$time")); Groupdate.put ("Month",NewBasicdbobject ("$month", "$time")); Groupdate.put ("Day",NewBasicdbobject ("$dayOfMonth", "$time")); Group.put ("$group",NewBasicdbobject ("_id", Groupdate));
== Output.results (). iterator ();
The first time to write a blog, only to find language organization ability really poor, exhausted, work ~ ~ ~
For more aggregation operations see this "http://docs.mongodb.org/manual/reference/operator/aggregation-pipeline/"
MongoDB Group Statistics Group