Create database node.js with mongodb

It depends on what you're using to interact with mongo, but you don't usually need to create the database. It's usually just created when you use it in your code.

For example if you use the mongodb native driver:

MongoClient.connect('mongodb://localhost:27017/myNewDatabase', function(err, db) {
  // If a database called "myNewDatabase" exists, it is used, otherwise it gets created.

  var collection = db.collection('myNewCollection');
  // If a collection called "myNewCollection" exists, it is used, otherwise it gets created.
});

The driver api docs have lots of examples - http://mongodb.github.io/node-mongodb-native/2.0/api/


you can do it this way:

var MongoClient = require('mongodb').MongoClient;
var url = "mongodb://localhost:27017/mydatabase"; // mydatabase is the name of db 
MongoClient.connect(url, function(err, db) {
  if (err) throw err;
console.log("Database created!");
  db.close();
});