db.collection不是函数?什么?

时间:2017-12-28 13:54:39

标签: mongodb

我正在学习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] } } }

3 个答案:

答案 0 :(得分:5)

新版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/

的更多文档

答案 1 :(得分:2)

我跟着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();
  });
}); 

答案 2 :(得分:0)

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

npm uninstall mongodb  
npm install mongodb@2.2.33

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