在 mongodb 中获取集合的键/列 - nodejs [英] Getting keys/columns of collection in mongodb - nodejs

查看:82
本文介绍了在 mongodb 中获取集合的键/列 - nodejs的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

};

但我收到以下错误:

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?

推荐答案

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

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. 切换到 Mongolian DeadBeef(我知道这名字不好).
  2. 试试命令 executeDbCommand.
  1. Switch to Mongolian DeadBeef (bad name, I know).
  2. Try the command executeDbCommand.

祝你好运!

这篇关于在 mongodb 中获取集合的键/列 - nodejs的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆