Reputation: 3457
This is my collection named logins
db.logins.find().pretty()
"cust_id" : "testuser",
"created_at" : "2011-03-11 10:31:02.765"
"cust_id" : "testuser",
"created_at" : "2011-03-11 10:31:02.765"
I am trying to find out Logins of each user based on a Date
I tried this way
var m = function() {
var aux = this.created_at.indexOf(' ');
emit({cust:this.cust_id,daterr:this.created_at.substring(0,aux)},1);
}
var r = function(k, values) {
var l = values.length;
var r = 0;
var i;
for (i=0; i<l; i++) {
r+=l;
}
return r;
}
q = function() {
var query = {"created_at": {$lt: "2013-04-30 11:19:52.587"}}
return query;
}
db.logins.mapReduce(m, r, { query : q(), out : "userLoginCountMonthly" });
I am getting the following Output
db.userLoginCountMonthly.find().pretty()
{ "_id" : { "cust" : "testuser", "daterr" : "2011-03-11" }, "value" : 4 }
What i see is that the value must be 2 , but it is shown as 4
Could you please tell me why its displaying wrong ??
Upvotes: 0
Views: 60
Reputation: 180917
I may very well be totally off in this since I've never used MongoDB's mapreduce and I'm not quite sure what you're trying to achieve in the reduce, but this looks wrong;
var l = values.length;
var r = 0;
var i;
for (i=0; i<l; i++) {
r+=l;
}
return r;
If values.length is 2, this will loop twice, each time adding 2, returning 4.
As far as I can see since you only want a count, a simple return values.length
would make more sense as a reduce function.
Upvotes: 1