Quick guide: how to create a table in MongoDB using Node.js

February 04, 2022

Tags: Technologies
nodejs
Unsplash

 

Node.Js is an open-source, cross-platform, back-end JavaScript runtime environment that runs on the V8 engine and executes JavaScript code outside of a web browser.

 

It is used in most cases to build BackEnd services, better known as APIs (Application Programming Interfaces), these are the services that give power to web applications running inside a browser or to a mobile application running inside a mobile device. 

 

What is MongoDB

 

According to the definition on its official page “MongoDB is a document database, which means that it stores data in JSON-like documents. We believe this is the most natural way to think about data and is much more expressive and powerful than the traditional row/column model.”

 

The traditional way data is stored and used is structured in a table or row and column format, but this is not the case in real-time. MongoDB stores data in real-time in this way using documents similar to JSON and NoSQL.

 

Create a table in MongoDB using Node.js

 

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

 

Step 1: Start MongoDB

 

To start, write the code:

 

sudo service mongod start

 

Step 2: Get the base URL

 

In order to find the base URL of the MongoDB service, first, open a terminal and run Mongo Shell.

 

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]

 

Doing this causes Mongo Shell to repeat the base URL of MongoDB on startup

 

mongodb://127.0.0.1:27017

 

Step 3: Now, the full URL is prepared

 

You have to make sure you have the full URL. To the base URL, add the name of the database you want to create (newdb)

 

mongodb://127.0.0.1:27017/newdb

 

Step 4: Create a MongoClient

 

First, a Mongo::Client object is created to bind to a MongoDB implementation, this is an important step in connecting MongoDB with NodeJS.

 

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

 

Step 5: Bind to the server

 

Link MongoClient to MongoDB server

 

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

 

Step 6: Create a MongoDB Pool

 

The syntax of the createCollection function is as follows:

 

db.createCollection(<collection_name>, <callback_function>)

 

Now that we've covered all the essential steps to create the table, let's compile them together:

 

// Script.js

// we create 'users' collection in newdb database
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 (error) throw error;
    // db pointing to newdb
    console.log("Switched to "+db.databaseName+" database");
    // create 'users' collection in newdb database
    db.createCollection("users", function(err, result) {
        if (error) throw error;
        console.log("Collection is created!");
        // close the connection to db when you are done with it
        db.close();
    });
});

 

With this, it is possible to create a table in MongoDB using NodeJS.

 

We recommend you on video


 

Yes, I liked thiscontent.