1

I want to create a new Database in MongoDB using the Node JS driver. I tried the following approaches, but none of them created any databases (I checked using the mongo shell and RoboMongo) and the worst part is, it is not showing any errors, below programs are executed successfully without any error (I mean, error is NULL)

  • First Method, using the Mongo Server

var Db = require('mongodb').Db, 
Server = require('mongodb').Server;

var db = new Db('myNewDatabase', new Server('localhost', 27017));
db.open(function (err, db) {
  if (err) {
  	console.dir('ERROR we are in the callback of the open ');
  	console.dir(err);

  	throw err;
  }	

  // Use the admin database for the operation
  var adminDb = db.admin();

  console.dir('we are in the callback of the open');
  db.close();
 
});

  • Second approach I followed is:

var server = "localhost";
var port   = 27017; 
var dbName = "myNewDatabase";
var mongodb          = require('mongodb');
var mongoClient = mongodb.MongoClient;
var connString = "mongodb://"+server+":"+port+"/"+dbName;

mongoClient.connect(connString, function(err, db) {
    console.dir(err);
    if(!err) {
        console.log("\nMongo DB connected\n");          
        db.collection('test_correctly_access_collections', function(err, col2) {
        	console.dir(err);
			if(err) {
				console.dir('Thier is a error in creating collection');
				console.dir(err);
			}
			console.log("\nColllection created succesfully\n");          
			db.close();
        });	
    }
    else{
        console.log("Mongo DB could not be connected");
        process.exit(0);
    }
});

According to this link, we can use getDatabase API to create a new database, i tried for the same API in the Node JS, but i am unable to find one.

1
  • 1
    i think for new database you need to insert a document in a collection so mongodb will create the collection and then the database. Actually this db.collection('test_correctly_access_collections' does not create the collection Commented Jan 4, 2016 at 2:47

1 Answer 1

1

As searched in google and stackOverflow for this question, but I am able to find, only very few. So i am posting the answer to this myself.

At first thank you @somallg , you are right i voted up your comment.

Answer is, you need to insert document into collection and then, MongoDB will create the New Database and also the collection. So, above approaches in my question can we re-written as follows to create a new database using the Node JS Driver:

  • First Appoarch

var Db = require('mongodb').Db, 
Server = require('mongodb').Server;

var db = new Db('myNewDatabase', new Server('localhost', 27017));

db.open(function (err, db) {
  if (err) {
  	console.dir('ERROR we are in the callback of the open ');
  	console.dir(err);

  	throw err;
  }	

   var collection = db.collection("simple_document_insert_collection_no_safe");
   collection.insert({hello:'world_no_safe'});

  console.dir('we are in the callback of the open');
  db.close();
 
});

  • second approach

var server = "localhost";
var port   = 27017; 
var dbName = "myNewDatabase";
var mongodb          = require('mongodb');
var mongoClient = mongodb.MongoClient;
var connString = "mongodb://"+server+":"+port+"/"+dbName;

mongoClient.connect(connString, function(err, db) {
    console.dir(err);
    if(!err) {
    	var collection = db.collection("simple_document_insert_collection_no_safe");
    	collection.insert({hello:'world_no_safe'});

    }
    else{
        console.log("Mongo DB could not be connected");
        process.exit(0);
    }
    db.close();
});

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.