Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

mongodb map reduce to get uniques by date/day

Tags:

mongodb

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.

like image 433
Anand Avatar asked Apr 23 '12 16:04

Anand


People also ask

What is MAP-reduce function in MongoDB?

Map-reduce is a data processing paradigm for condensing large volumes of data into useful aggregated results. To perform map-reduce operations, MongoDB provides the mapReduce database command.

What is the difference between MAP-reduce function and aggregate function?

Map-reduce is a common pattern when working with Big Data – it's a way to extract info from a huge dataset. But now, starting with version 2.2, MongoDB includes a new feature called Aggregation framework. Functionality-wise, Aggregation is equivalent to map-reduce but, on paper, it promises to be much faster.

What is emit in Map-Reduce?

The map function may optionally call emit(key,value) any number of times to create an output document associating key with value .

How do I search in MongoDB?

Find() Method. In MongoDB, find() method is used to select documents in a collection and return a cursor to the selected documents. Cursor means a pointer that points to a document, when we use find() method it returns a pointer on the selected documents and returns one by one.


2 Answers

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" }
             }
     );
    }
};
like image 154
Anton Avatar answered Oct 09 '22 09:10

Anton


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.

like image 29
mixdev Avatar answered Oct 09 '22 11:10

mixdev