MongoDB. Reduce card close function

I want to calculate users accumulated by dates. I have the following map reduction features:

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 I start, I get an error message:

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
}

It seems impossible to call db. methods inside finalization. Why?

I knoq, which I can call from map () and from reduce ()

+3
source share
2 answers

Yes, you cannot run queries in map reduction functions. They should depend only on their input parameters.

+4
source

You can run queries in mapreduce functions, namely that db is a link to the default database, and not automatically to the database with which you are working.

, : var fdb = db.getSiblingDB('yourdb'); var results = fdb.yourcollection.find(..);

, . "can" "should".

0

All Articles