【问题标题】:db.collection is not a function? What?db.collection 不是函数?什么?
【发布时间】:2018-06-09 01:52:00
【问题描述】:

我正在学习 mongodb 并使用以下代码跟随教程:

我的 index.js 文件:

var MongoClient = require('mongodb').MongoClient;

var url = 'mongodb://u****:p****@ds131687.mlab.com:31687/learning_mongo';

var findDocuments = function(db, callback) {
    var collection = db.collection('tours');

    collection.find().toArray(function(err,docs){
        if (err) throw err;
        console.log(docs);
        callback;
    })

}

MongoClient.connect(url, function(err, db){
    if (err) throw err;
    // console.log("it is working");
    // db.close();
    findDocuments(db, function(){
        db.close();
    });
})

不幸的是,我在终端中收到以下错误:

dosstx:~/workspace $ node index.js
/home/ubuntu/workspace/node_modules/mongodb/lib/mongo_client.js:810
        throw err;
        ^

TypeError: db.collection is not a function
    at findDocuments (/home/ubuntu/workspace/index.js:6:25)
    at /home/ubuntu/workspace/index.js:20:5
    at args.push (/home/ubuntu/workspace/node_modules/mongodb/lib/utils.js:404:72)
    at /home/ubuntu/workspace/node_modules/mongodb/lib/mongo_client.js:255:5
    at connectCallback (/home/ubuntu/workspace/node_modules/mongodb/lib/mongo_client.js:933:5)
    at /home/ubuntu/workspace/node_modules/mongodb/lib/mongo_client.js:807:13
    at _combinedTickCallback (internal/process/next_tick.js:73:7)
    at process._tickCallback (internal/process/next_tick.js:104:9)

是否有人对如何继续和/或导致错误的原因有任何建议? 教程作者没有收到此错误,我看不出还有什么不同之处(其他比可能不同版本的 MongoDB 和作者的版本?)

console.log(db) 显示:

    MongoClient {
  domain: null,
  _events: {},
  _eventsCount: 0,
  _maxListeners: undefined,
  s: 
   { url: 'mongodb://****:****@ds131687.mlab.com:31687/learning_mongo',
     options: 
      { user: *****,
        password: ****,
        socketOptions: {},
        read_preference_tags: null,
        readPreference: [Object],
        dbName: 'learning_mongo',
        servers: [Object],
        auth: [Object],
        server_options: [Object],
        db_options: [Object],
        rs_options: [Object],
        mongos_options: [Object],
        socketTimeoutMS: 360000,
        connectTimeoutMS: 30000,
        promiseLibrary: [Function: Promise] },
     promiseLibrary: [Function: Promise],
     dbCache: {},
     sessions: [] },
  topology: 
   Server {
     domain: null,
     _events: 
      { serverOpening: [Function],
        serverDescriptionChanged: [Function],
        serverHeartbeatStarted: [Function],
        serverHeartbeatSucceeded: [Function],
        serverHeartbeatFailed: [Function],
        serverClosed: [Function],
        topologyOpening: [Function],
        topologyClosed: [Function],
        topologyDescriptionChanged: [Function],
        joined: [Function],
        left: [Function],
        ping: [Function],
        ha: [Function],
        authenticated: [Function],
        error: [Function],
        timeout: [Function],
        close: [Function],
        parseError: [Function],
        open: [Object],
        fullsetup: [Object],
        all: [Object],
        reconnect: [Function] },
     _eventsCount: 22,
     _maxListeners: undefined,
     clientInfo: 
      { driver: [Object],
        os: [Object],
        platform: 'Node.js v6.11.2, LE' },
     s: 
      { coreTopology: [Object],
        sCapabilities: null,
        clonedOptions: [Object],
        reconnect: true,
        emitError: true,
        poolSize: 5,
        storeOptions: [Object],
        store: [Object],
        host: 'ds131687.mlab.com',
        port: 31687,
        options: [Object],
        sessionPool: [Object],
        promiseLibrary: [Function: Promise] } } }

【问题讨论】:

标签: mongodb


【解决方案1】:

db.collection() 不是最新版本的 MongoDB 中的函数。先卸载最新版本的MongoDB,再安装2.2.33版本的MongoDB。

npm uninstall mongodb  
npm install mongodb@2.2.33

版本 MongoDB >= 3 - 该数据库变量实际上是您尝试访问的对象的父对象。

【讨论】:

    【解决方案2】:

    在新版本的 mongodb 上连接功能发生了变化。这应该工作

    var MongoClient = require('mongodb').MongoClient;
    
    var url = 'mongodb://u****:p****@ds131687.mlab.com:31687/learning_mongo';
    
    var findDocuments = function(db, callback) {
        var collection = db.collection('tours');
    
        collection.find().toArray(function(err,docs){
            if (err) throw err;
            console.log(docs);
            callback;
        })
    
    }
    
    MongoClient.connect(url, function(err, client){
        if (err) throw err;
        // console.log("it is working");
        // db.close();
        findDocuments(client.db('learning_mongo'), function(){
            db.close();
        });
    })
    

    更多关于http://mongodb.github.io/node-mongodb-native/3.0/api/的文档

    【讨论】:

      【解决方案3】:

      我关注this question 并能够解决错误。基本上,我必须在我的 index.js 文件中添加以下修改:

          MongoClient.connect('mongodb://localhost', function (err, client) {
        if (err) throw err;
      
        var db = client.db('mytestingdb');
      
        db.collection('customers').findOne({}, function (findErr, result) {
          if (findErr) throw findErr;
          console.log(result.name);
          client.close();
        });
      }); 
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2021-11-17
        • 2018-06-04
        • 1970-01-01
        • 1970-01-01
        • 2021-11-05
        • 2021-10-31
        相关资源
        最近更新 更多