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

Difference between revisions of "Sum and Count"

From NoSQLZoo
Jump to: navigation, search
Line 4: Line 4:
 
<code>count()</code> is a cursor method that takes a query and returns a number equal to the amount of documents that matched the query.<br/>
 
<code>count()</code> is a cursor method that takes a query and returns a number equal to the amount of documents that matched the query.<br/>
 
<code>$sum</code> is an aggregation operator availible in the <code>$group</code> stage, that can be used to both sum values and count the number of documents.<br/>
 
<code>$sum</code> is an aggregation operator availible in the <code>$group</code> stage, that can be used to both sum values and count the number of documents.<br/>
mapReduce can produce a sum or a count during the results stage by using JavaScript.
+
<code>mapReduce</code> can produce a sum or a count during the results stage by using JavaScript.
</div>
 
<div class="q nonum" data-lang="mongo">.count()
 
<div class="def">db.world.count({"continent":"Africa"})
 
 
</div>
 
</div>
 +
<div class="q nonum" data-lang="mongo"><code>.count()</code>
 +
<div class="def">db.world.count({"continent":"Africa"})</div>
 
</div>
 
</div>
  
<div class="q nonum" data-lang="mongo">$sum
+
<div class="q nonum" data-lang="mongo"><code>$sum</code>
 
<div class="def">
 
<div class="def">
 
db.world.aggregate([
 
db.world.aggregate([
    {"$group":{
+
  {"$group":{
        "_id":"$continent",
+
      "_id": "$continent",
        "sum of populations":{$sum:"$population"},
+
      "sum of populations": {$sum: "$population"},
        "count of countries":{$sum:1}
+
      "count of countries": {$sum: 1}
    }}
+
  }}
 
])
 
])
 
</div>
 
</div>
 
</div>
 
</div>
  
<div class="q nonum" data-lang="mongo">Array.sum()
+
<div class="q nonum" data-lang="mongo"><code>Array.sum()</code>
 
<div class="def">
 
<div class="def">
 
db.world.mapReduce(
 
db.world.mapReduce(
    function(){emit(this.continent, this.population)},
+
  function(){
    function(k,v){ return Array.sum(v) },
+
    emit(this.continent, this.population);
     {out:{inline:1}}
+
  },
 +
  function(k,v){  
 +
    return Array.sum(v);
 +
  },
 +
  {
 +
     out:{inline:1}
 +
  }
 
)
 
)
 
</div>
 
</div>
 
</div>
 
</div>

Revision as of 03:35, 6 April 2018

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()
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}
 }

)