Anand
Anand

Reputation: 4232

mongodb map reduce to get uniques by date/day

trying to group by date and number

my document

 { "_id" : ObjectId("4f956dee76ddb26752026e8f"), "request" : "default", "options" : "1", "keyword" : "somekey", "number" : "5b234b79-4d70-437e-8eef-32a2941af40a", "date" : "20120423200446", "time" : 1335193066 }

my query

map = "function() { var date = new Date(this.time * 1000); var key = date.getFullYear() + date.getMonth() + date.getDate(); emit({day : key, number: this.number}, {count: 1}); }"

reduce = "function(key, values) { var count = 0; values.forEach(function(v) { count += v['count']; }); return {count: count}; }"

db.txtweb.mapReduce(map, reduce, {out: "pageview_results"});

my error

uncaught exception: map reduce failed:{ "errmsg" : "ns doesn't exist", "ok" : 0 }

I cannot figure out whats wrong, but I think it is do something with the date functionality.

Any ideas.

Upvotes: 15

Views: 15372

Answers (2)

Anton
Anton

Reputation: 6041

ns doesn't exist means that you're accessing a non-existent database or collection. Check their names

Script below group this.amount by day (without time)

     db.payments.mapReduce(
             function() {
                 var k = new Date(this.payment_time);
                 k.setMinutes(0);
                 k.setSeconds(0);
                 k.setMilliseconds(0);
                 emit(k, this.amount);
             }, 
             function (key, vals) {
                 var total = 0;
                 for (var i = 0; i < vals.length; i++) {
                     total += vals[i];
                 }
                 return total;
             },
             {
              out : {merge : "resultName" }
             }
     );
    }
};

Upvotes: 29

mixdev
mixdev

Reputation: 2844

This happens mostly when you run a script and forgot to select the database

use your_database_name;

Note the line to select the database.

Upvotes: 5

Related Questions