2

I am trying to get all the columns/keys of an collection using the following code:

var Db          = require('mongodb').Db; //for mongodb
var Connection  = require('mongodb').Connection;
var Server      = require('mongodb').Server;
var BSON        = require('mongodb').Server;
var ObjectID    = require('mongodb').ObjectID;


DbModels = function(host, port){
  this.db = new Db('test', new Server(host, port,{auto_reconnect:true},{}));
  this.db.open(function(){});
};

DbModels.prototype.testDb=function(_collection){
    console.log("Collection:");
    console.log(this.db.collection('persons').find());
    var mr = this.db.runCommand({
      "mapreduce":"br_205_results",
      "map" : function(){
          for (var key in this){emit(key, null);}
        },
      "reduce" : function(key, stuff){
          return null;
        }
      });
      console.log(mr);
    db[mr.result].distinct("_id");

};

But I get the following error:

TypeError: Object [object Object] has no method 'runCommand'
    at [object Object].testDb (c:\Program Files\nodejs\DbModels.js:17:22)
    at c:\Program Files\nodejs\socketio.js:164:14
    at callbacks (c:\Program Files\nodejs\node_modules\express\lib\router\index.js:272:11)
    at param (c:\Program Files\nodejs\node_modules\express\lib\router\index.js:246:11)
    at pass (c:\Program Files\nodejs\node_modules\express\lib\router\index.js:253:5)
    at Router._dispatch (c:\Program Files\nodejs\node_modules\express\lib\router\index.js:280:4)
    at Object.handle (c:\Program Files\nodejs\node_modules\express\lib\router\index.js:45:10)
    at next (c:\Program Files\nodejs\node_modules\connect\lib\http.js:203:15)
    at Object.handle (c:\Program Files\nodejs\node_modules\now\lib\now.js:213:7)
    at next (c:\Program Files\nodejs\node_modules\connect\lib\http.js:203:15)

Any idea, how I can solve this?

Sergio Tulentsev
  • 226,338
  • 43
  • 373
  • 367
The Code Buccaneer
  • 775
  • 1
  • 10
  • 21

1 Answers1

2

The problem is that the MongoDB driver (mongodb-native) that you're using doesn't implement the interface that's found in the Mongo console. That's why you're seeing that error with runCommand. You have two choices:

  1. Switch to Mongolian DeadBeef (bad name, I know).
  2. Try the command executeDbCommand.

Good luck!

JP Richardson
  • 38,609
  • 36
  • 119
  • 151