0

I just started to try MongoDB. I got an error message TypeError: db.open is not a function. I can see new mongodb.Server and new mongodb.Db are working.

I'm using mongodb@3.0.2 node_modules/mongodb. Any suggestions?

module.exports = function () {
    /**
     * MongoDb database access
     *
     */
    var collections = {};
    var db;
    var mongodb = require('mongodb');
    var mongoDbConfig = {
        dbName: 'ngCodeCamper',
        host: 'localhost',
        port: 27017
    };

    // properties with getter functions for export
    Object.defineProperty(this, 'db', {
        get: function () {
            return db;
        }
    });
    Object.defineProperty(this, 'collections', {
        get: function () {
            return collections;
        }
    });

    openMongoDb();

    return {
        db: this.db,
        collections: this.collections
    };

    function openMongoDb() {
        /* jshint camelcase:false */
        var dbServer = new mongodb.Server(
            mongoDbConfig.host,
            mongoDbConfig.port,
            { auto_reconnect: true }
        );

        db = new mongodb.Db(mongoDbConfig.dbName, dbServer, {
            strict: true,
            w: 1,
            safe: true
        });

        console.log(db);

        // Open the DB then get the collections
        db.open(getAllCollections);

        function getAllCollections() {
            // Gets a handle for all of the collections.
            // We do this all at once to save effort later.
            var collectionNames = ['Persons', 'Rooms', 'Sessions', 'TimeSlots', 'Tracks'];
            var countDown = collectionNames.length;
            collectionNames.forEach(function (name) {
                db.collection(name, { strict: true }, function (err, collection) {
                    if (err) {
                        console.log('\n!!! Failed while getting MongoDb collections; is the MongoDb server running?');
                        throw new Error('Unable to locate collection ' + name + ': ' + err);
                    }
                    collections[name] = collection;
                    countDown -= 1;
                    if (countDown === 0) {
                        console.log('Retrieved collection handles: ' + collectionNames.join(', '));
                    }
                });
            });
        }
    }
};
Jeremy Thille
  • 26,047
  • 12
  • 43
  • 63
Tester
  • 798
  • 2
  • 12
  • 32

2 Answers2

0

There have been breaking API changes with v3.x release of mongodb package. And the API method Db.prototype.open has also been removed, that's why it is showing error because you are using mongodb@3.0.2 verson.
Reference : https://github.com/mongodb/node-mongodb-native/blob/HEAD/CHANGES_3.0.0.md#api-changes

0

Yup, you need to use a MongoClient object instead of a DB object. The DB object is part of the MongoClient, examples on the links mentioned above and on db.collection is not a function when using MongoClient v3.0

Wimpers
  • 1
  • 2