user663724
user663724

Reputation:

MongoDb : mapReduce out collection result

I have the below Logincount.js .

Please tell me how can i Include the Date Field also while creating the collection LoginCount ??

Right now the js file creates a collection with two fields namely _id and value

I want it to also create a field called date with yesterdays Date in it .

This is my js file

m = function() { emit(this.cust_id, 1); }

r = function (k, vals) { var sum = 0; for (var i in vals) { sum += vals[i]; } return sum; }

   q = function() {
        var currentDate = new Date();
        currentDate.setDate(currentDate.getDate()-1);
        var month = (currentDate.getMonth() < 9 ? "0"+ (currentDate.getMonth()+1) : (currentDate.getMonth()+1));
        var day = (currentDate.getDate() < 10 ? "0" + currentDate.getDate() : currentDate.getDate());
        var date = currentDate.getTime();
        var patt = date;
        var query = {"created_at":patt};
        return query;
}


res = db.user_logins.mapReduce(m, r, { query : q(), out :  "LoginCount" });

Upvotes: 1

Views: 866

Answers (1)

Johntron
Johntron

Reputation: 2653

As far as I know, the only fields you can have in a collection generated by MapReduce are _id and value – this is just the nature of MapReduce. Maybe you could run an update on your output collection that "flattens" the value field?

Upvotes: 1

Related Questions