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
m
Line 1: Line 1:
<pre class=setup>
 
#ENCODING
 
import io
 
import sys
 
sys.stdout = io.TextIOWrapper(sys.stdout.buffer, encoding='utf-16')
 
#MONGO
 
from pymongo import MongoClient
 
client = MongoClient()
 
client.progzoo.authenticate('scott','tiger')
 
db = client['progzoo']
 
#PRETTY
 
import pprint
 
pp = pprint.PrettyPrinter(indent=4)
 
#CODE
 
from bson.code import Code
 
</pre>
 
 
{{TopTenTips}}
 
{{TopTenTips}}
 
<div style="min-height:25em">
 
<div style="min-height:25em">
Line 22: Line 6:
 
mapReduce can produce a sum or a count during the results stage by using JavaScript.
 
mapReduce can produce a sum or a count during the results stage by using JavaScript.
 
</div>
 
</div>
<div class="q nonum" data-lang="py3">.count()
+
<div class="q nonum" data-lang="mongo">.count()
<div class="def">print(db.world.count({"continent":"Africa"}))
+
<div class="def">db.world.count({"continent":"Africa"})
 
</div>
 
</div>
 
</div>
 
</div>
  
<div class="q nonum" data-lang="py3">$sum
+
<div class="q nonum" data-lang="mongo">$sum
<div class="def">pp.pprint(list(
+
<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="py3">Array.sum()
+
<div class="q nonum" data-lang="mongo">Array.sum()
<div class="def">temp = db.world.map_reduce(
+
<div class="def">
     map=Code("function(){emit(this.continent, this.population)}"),
+
db.world.mapReduce(
     reduce=Code("""function(k,v){
+
     function(){emit(this.continent, this.population)},
                      return Array.sum(v)
+
     function(k,v){ return Array.sum(v) },
                  }
+
     {out:{inline:1}}
                """),
 
     out={"inline":1}
 
 
)
 
)
  

Revision as of 16:33, 3 December 2015

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

)

pp.pprint(temp['results'])