在mongodb中获取集合的键/列 - nodejs

时间:2012-01-31 21:06:32

标签: node.js mongodb

我正在尝试使用以下代码获取集合的所有列/键:

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");

};

但是我收到以下错误:

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)

任何想法,我怎么能解决这个问题?

1 个答案:

答案 0 :(得分:2)

问题是你正在使用的MongoDB驱动程序(mongodb-native)没有实现Mongo控制台中的接口。这就是你在runCommand看到错误的原因。你有两个选择:

  1. 切换到Mongolian DeadBeef(名字不好,我知道)。
  2. 尝试使用executeDbCommand命令。
  3. 祝你好运!