16

I am attempting to create a mongo instance however I am unable to access any of the helper methods from the mongodb nodejs driver.

My mongo instance is running within docker and the ports have been opened up to my local.

TypeError: db.createCollection is not a function
at /var/www/html/beacon/index.js:6:8
at args.push (/var/www/html/beacon/node_modules/mongodb/lib/utils.js:431:72)
at /var/www/html/beacon/node_modules/mongodb/lib/mongo_client.js:254:5
at connectCallback (/var/www/html/beacon/node_modules/mongodb/lib/mongo_client.js:933:5)
at /var/www/html/beacon/node_modules/mongodb/lib/mongo_client.js:794:11
at _combinedTickCallback (internal/process/next_tick.js:73:7)
at process._tickCallback (internal/process/next_tick.js:104:9)

Copied from w3schools...

var MongoClient = require('mongodb').MongoClient;
var url = "mongodb://localhost:27017/mydb";

MongoClient.connect(url, function(err, db) {
    if (err) throw err;
    db.createCollection("customers", function(err, res) {
        if (err) throw err;
        console.log("Collection created!");
        db.close();
    });
});

No error is returned through the run, and no methods are exposed on the db object.

any ideas?

2
  • Can you put aconsole.dir(db) in line 6, (before db.createCollection) and show here the result?
    – Elad
    Commented Dec 5, 2017 at 17:01
  • Possible duplicate of MongoDB nodeJS error
    – njLT
    Commented Dec 6, 2017 at 14:05

3 Answers 3

30

According to the changelog for Mongodb 3.0 you now get a client object containing the database object instead:

So you need the db object that points to the database you want to use, in your case mydb. Try this:

var url = "mongodb://localhost:27017/";
MongoClient.connect(url, function(err, db) {   //here db is the client obj
    if (err) throw err;
    var dbase = db.db("mydb"); //here
    dbase.createCollection("customers", function(err, res) {
        if (err) throw err;
        console.log("Collection created!");
        db.close();   //close method has also been moved to client obj
    });
});
2
  • Will mark as correct answer when the timing allows it. Odd in my opinion with the db url path however thank you :) Commented Dec 5, 2017 at 17:04
  • 3
    Surprised to see that this isn't documented on driver's page in examples, but only in change log.
    – A dev
    Commented May 3, 2018 at 14:24
3

You're not the one facing this issue. Seems that 3.0 mongo driver has a bug or these are just breaking backwards compatibility changes. Take a look here: db.collection is not a function when using MongoClient v3.0

To use DB name in the URL, you need to uninstall MongoDB, change to "mongodb": "^2.2.33" in dependencies and do npm install to install the new version.

Or you can install specific version with command npm install [email protected] --save

1
  • Not sure if you are actually answering the question
    – bichito
    Commented Dec 6, 2017 at 21:33
1

I was getting the error while running createCollection() from the command line Mongo shell tool mongosh.

TypeError: db.createCollection is not a function

In my case, I could not get create collection to work, so I resorted to just inserting a document into the collection which I wanted to create. By default, Mongo will create a collection by that name on the fly, assuming you have the permissions to do so. This is not an ideal solution, but it allowed me to proceed at least.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.