Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

db.collection is not a function? What?

Tags:

mongodb

I am learning mongodb and following a tutorial with the below code:

My index.js file:

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

Unfortunately, I get the following error in terminal:

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)

Does anyone have any advice on how to proceed and/or what is causing the error? The tutorial author does not get this error and I can't see what else is different for me (other than possible different versions of MongoDB vs the author's?)

console.log(db) shows:

    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] } } }
like image 282
redshift Avatar asked Nov 04 '25 09:11

redshift


1 Answers

The connect function has changed on new version of mongodb. This should work

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

More documentation on this http://mongodb.github.io/node-mongodb-native/3.0/api/

like image 157
Priidik Vaikla Avatar answered Nov 06 '25 03:11

Priidik Vaikla



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!