C B
C B

Reputation: 13344

Mongo NodeJS map reduce

Can't get this working.. What am I missing here.. NodeJS, Mongoskin.. I cannot get the results of the mapreduce. DB and collection seems ok.

var mongo = require('mongoskin');
var db = mongo.db('localhost:27017/testdb?auto_reconnect=true&poolSize=5');

db.collection('users');
db.bind('users');

db.users.find().sort({userid: -1}).skip(0).limit(0).toArray(function(err, users) {
        //console.log(err, users);
});

m = function() {
        emit( this.userid, this);
}
r = function(k, v) {
        return {k: v}
}
db.users.mapReduce(m, r, {out: 'coll'}, function(e, c) {
                console.log(c);
                process.exit(1);            
});

Upvotes: 2

Views: 3325

Answers (2)

Andrew Tomczak
Andrew Tomczak

Reputation: 9

I needed to prefix the connection string as:

var db = mongo.db('mongodb://localhost:27017/testdb?auto_reconnect=true&poolSize=5');

Upvotes: -1

beny23
beny23

Reputation: 35068

You'll need to pass the map and reduce functions as strings to mongodb, so that they're executed in the server:

db.users.mapReduce(m.toString(), r.toString(), {out: 'coll'}, function(e, c) {
                console.log(c);
                process.exit(1);            
});

Upvotes: 8

Related Questions