db.createCollection is not a function
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
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
});
});
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.