In this Chapter , we will create database demo_db
in the NoSql , Document oriented
database MongoDb
using Node.js
Step - 1 : Installing Packages
We will start by
installing all the required packages from npm.
We are using the following package in our application :
var mongo = require('mongodb');
npm install mongodb --save
Step - 2 : Establish Connection
Now let's establish a connection between the mongoDb Database and our node.js application.
var new_db = "mongodb://localhost:27017/demo_db"
Step - 3 : Create the database
Connect() is an inbuilt method used to is an inbuilt method used to Creates a
connection to a MongoDB instance and returns the reference to the database.
It instantiates a new connection to the MongoDB
instance running on the localhost interface and returns a reference to demo_db
//create_database_mongo_nodejs.js
mongo.connect(new_db ,(error , db) => {
if (error){
throw error;
}
console.log("Database demo_db created successfully");
//To close the connection
db.close();
});
D:\nj-learn-mongo>node create_database_mongo_nodejs.js
D:\nj-learn-mongo>node create_database_mongo_nodejs.js
Database demo_db created successfully