:::: MENU ::::

Monthly Archives / November 2015

  • Nov 26 / 2015
  • 0
Database

Gather/Aggregate results with MongoDB

Need to gather or aggregate some results with MongoDB? Easy!

You can use the group command over any collection to get a nice display of your results aggregated:

db.collection.group(
   {
     key: { document: 1},
     cond: { },
     reduce: function( curr, result ) {
                 result.total += 1;
             },
     initial: { total : 0 }
   }
)

You will get something like:

[
	{
		"document" : "my_first_document",
		"total" : 44124
	},
	{
		"document" : "my_second_example",
		"total" : 3503
	},
	{
		"document" : "still_another_one",
		"total" : 3928
	}
]

Here you are!

Question ? Contact