简体   繁体   中英

How to create New MongoDb Database using the Node JS driver

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.

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(); }); 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM