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.
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.
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.
The map function may optionally call emit(key,value) any number of times to create an output document associating key with value .
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.
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" }
}
);
}
};
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With