Cookies help us deliver our services. By using our services, you agree to our use of cookies. More information

Sum and Count

From NoSQLZoo
Jump to: navigation, search

There are many ways to do this in MongoDB.

count() is a cursor method that takes a query and returns a number equal to the amount of documents that matched the query.
$sum is an aggregation operator availible in the $group stage, that can be used to both sum values and count the number of documents.
mapReduce can produce a sum or a count during the results stage by using JavaScript.

.count(){"continent": "Africa"});
  {"$group": {
      "_id": "$continent",
      "sum of populations": {$sum: "$population"},
      "count of countries": {$sum: 1}
  function () {
    emit(this.continent, this.population);
  function (k, v) {
    return Array.sum(v);
    out: {inline: 1}