Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MongoDB. Map-Reduce finalize function

I want to calculate users with accumulating by dates. I have the following map reduce functions:

var m = function(){
    // creation date
    var d = new Date(parseInt(this._id.toString().slice(0,8), 16)*1000);
    // ticks
    var t = d.getTime();
    emit(d2,d3);
};

var r = function(k,v){
    return v[0];    // just go next with ticks
};

var opts = { out :  { merge : "UserAccum", db : "Metric"},
    finalize: function(k,v){
    var str = "parseInt(this._id.toString().slice(0,8), 16)*1000 <= "+v;
    return db.User.count({$where:str});         
} 
};
var res = db.Provider.mapReduce(m, r, opts);

When run, I get error:

Sat May 12 17:47:23 uncaught exception: map reduce failed:{
    "assertion" : "invoke failed: JS Error: TypeError: db has no properties
nofile_b:2",
    "assertionCode" : 9004,
    "errmsg" : "db assertion failure",
    "ok" : 0
}

Looks like it's not possible to call db. methods inside finalize. Why?

I knoq that I can call from map() and from reduce()

like image 489
bug0r Avatar asked Jul 25 '26 12:07

bug0r


1 Answers

Yes, you can't run queries in map-reduce functions. They must depend only on their input parameters.

like image 106
Sergio Tulentsev Avatar answered Jul 28 '26 04:07

Sergio Tulentsev