Signup/Sign In
LAST UPDATED: MAY 7, 2021

Create MongoDB DataBase with NodeJs

We will discuss how to create a MongoDB database with Node.js in this Tutorial. We'll look at how to import data into the database, how to use the MongoDB driver for Node.js. We'll finish up by creating a quick example that uses Node Express and MongoDB to store and serve content.

Let's dive straight into it.

Node.js MongoDB Create Database

To build a database in MongoDB, first, create a MongoClient object, then define a link URL that includes the correct IP address and the database name.

If the database does not exist, MongoDB can build it and bind to it.

Start MongoDB

Before Creating Database first starts MongoDB Service, type the following code to the terminal to start the MongoDB.

sudo service mongod start

Get the MongoDB Service's base URL

To find the MongoDB Service's base url, open a Terminal and run Mongo Shell. We will use this URL to connect with the database.

Shashank@nodejs:~$ mongo
MongoDB shell version v3.4.9
connecting to: mongodb://127.0.0.1:27017
MongoDB server version: 3.4.9
Server has startup warnings: 
2017-10-29T18:15:36.110+0530 I STORAGE  [initandlisten]

The base URL of MongoDB is echoed back by the Mongo Shell as it starts up.

mongodb://127.0.0.1:27017

Prepare the complete URL

Make sure you have the full URL. To the base URL, append the database name you want to build (for example, newdb).

mongodb://127.0.0.1:27017/newdb

Create a MongoClient

Now, Build a Mongo::Client object to bind to a MongoDB deployment. That's an important step to connect out MongoDB with Node.js in our application as we'll see in next step.

var MongoClient = require('mongodb').MongoClient;

Binding To Server

With the support of the URL, bind MongoClient to the MongoDB Server.

MongoClient.connect(mongodb://127.0.0.1:27017/newdb, function(err, db));

The dB object points to the newly generated database newdb if the relationship is good.

Example: Node.js MongoDB Create Database

Now, we have seen all the essential steps to create our database in our MongoDB application, now let's compile it together.

// Script.js

// newdb is the new database we create
var url = "mongodb://localhost:27017/newdb";
 
// create a client to mongodb
var MongoClient = require('mongodb').MongoClient;
 
// make client connect to mongo service
MongoClient.connect(url, function(err, db) {
    if (err) throw err;
    console.log("Database created!");
    // print database name
    console.log("db object points to the database : "+ db.databaseName);
    // after completing all the operations with db, close it.
    db.close();
});

Output:

shashank@tutorialkart:~/workspace/nodejs/mongodb$ node script.js 
Database created!
db object points to the database : newdb

Conclusion

Finally, at the end of the tutorial, we looked at MongoDB usability, as well as how to use Node.js to build a database and insert data into it. Keep following for more excellent as well as informative tutorials.



About the author: