javascript - Getting a count of Documents (Rows) using mapReduce in MongoDb -


i hate re-post stuff feel though i've typed same syntax of issues i've looked up. i'm trying map down count of how many rows there price greater 9000. can tell me i'm doing wrong?

var map1 = function(){     if(this.price > 9000) {         emit(this._id, {count: 1});     } };  var red1 = function(keyid, values){     var count = 0;     values.foreach(function(v) {             count +=v['count'];             });      return{count: count}; }  db.plots.mapreduce(map1, red1, {out: "query1"}) 

my results end this...

> db.query1.find() { "_id" : objectid("5170609afa9e8646fc906815"), "value" : { "count" : 1 } } { "_id" : objectid("5170609afa9e8646fc906816"), "value" : { "count" : 1 } } { "_id" : objectid("5170609afa9e8646fc90681c"), "value" : { "count" : 1 } } { "_id" : objectid("5170609afa9e8646fc906834"), "value" : { "count" : 1 } } { "_id" : objectid("5170609afa9e8646fc90683e"), "value" : { "count" : 1 } } { "_id" : objectid("5170609afa9e8646fc906846"), "value" : { "count" : 1 } } { "_id" : objectid("5170609afa9e8646fc90684b"), "value" : { "count" : 1 } } { "_id" : objectid("5170609afa9e8646fc90684e"), "value" : { "count" : 1 } } { "_id" : objectid("5170609afa9e8646fc906851"), "value" : { "count" : 1 } } { "_id" : objectid("5170609afa9e8646fc90685a"), "value" : { "count" : 1 } } { "_id" : objectid("5170609afa9e8646fc906861"), "value" : { "count" : 1 } } { "_id" : objectid("5170609afa9e8646fc906864"), "value" : { "count" : 1 } } { "_id" : objectid("5170609afa9e8646fc906879"), "value" : { "count" : 1 } } { "_id" : objectid("5170609afa9e8646fc906882"), "value" : { "count" : 1 } } { "_id" : objectid("5170609afa9e8646fc906883"), "value" : { "count" : 1 } } { "_id" : objectid("5170609afa9e8646fc90688b"), "value" : { "count" : 1 } } { "_id" : objectid("5170609afa9e8646fc90688c"), "value" : { "count" : 1 } } { "_id" : objectid("5170609afa9e8646fc906891"), "value" : { "count" : 1 } } { "_id" : objectid("5170609afa9e8646fc906894"), "value" : { "count" : 1 } } { "_id" : objectid("5170609afa9e8646fc90689a"), "value" : { "count" : 1 } } 

what single number back.

you doing several things wrong - emitting unique key per document: since reduce combines documents same key getting no aggregations, comparing each value 9000 instead of using query option map/reduce.

i'm not sure why using map/reduce - simple aggregation framework:

db.plots.aggregate([ {$match:{price:{$gt:9000}}},                       {$group:{_id:null,count:{$sum:1}}} ] ); 

if have reason map reduce recommend following:

map = function() { emit(1, 1); } reduce = function( key, values ) {     var count = 0;     values.foreach(function(v) {             count +=v;     });      return count; }  db.plots.mapreduce(map, reduce, {out: "query1", query:{price:{$gt:9000}}}); 

Comments

Popular posts from this blog

c# - Send Image in Json : 400 Bad request -

jquery - Fancybox - apply a function to several elements -

An easy way to program an Android keyboard layout app -