NoSQLZoo

Sum and Count

Top Tips
Find
Sort
Sum and Count
Round
Join
Iterate
MapReduce

There are many ways to do this in MongoDB.

.count()

db.world.count({"continent": "Africa"});

$sum

db.world.aggregate([
  {"$group": {
      "_id": "$continent",
      "sum of populations": {$sum: "$population"},
      "count of countries": {$sum: 1}
  }}
]);

Array.sum()

db.world.mapReduce(
  function () {
    emit(this.continent, this.population);
  },
  function (k, v) {
    return Array.sum(v);
  },
  {
    out: {inline: 1}
  }
);