Create table in MongoDB using NodeJS
Here, we will discuss how to create a MongoDB TABLE/COLLECTION using db.createCollection() method with NodeJS in this Tutorial. 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.
NodeJS MongoDB Create Collection
To build a collection in MongoDB, first, create a MongoClient object, then define a link URL that includes the correct IP address and the database name.
Step 1: Start MongoDB.
To start MongoDB Service, type the following code.
sudo service mongod start
Step 2: Get the MongoDB Service's base URL.
To find the MongoDB Service's base URL, 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]
The base URL of MongoDB is echoed back by the Mongo Shell as it starts up.
mongodb://127.0.0.1:27017
Step 3: Prepare the complete URL
Make sure you have the full URL. To the base URL, append the database name you want to build (newdb).
mongodb://127.0.0.1:27017/newdb
Step 4: 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 NodeJS in our application.
var MongoClient = require('mongodb').MongoClient;
Step 5: 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.
Step 6: Create a MongoDB Set.
The syntax of the createCollection() function, which is used to build collections in MongoDB from NodeJS, is as follows.
db.createCollection(<collection_name>, <callback_function>)
collection_name |
The name of the latest MongoDB Collection that we want to make |
callback_function |
After Node has attempted to create a list and is ready with the output, this NodeJS Callback Function is renamed. |
Example
Now, we have seen all the essential steps to create TABLE in our MongoDB database, now let's compile it 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 (err) throw err;
// db pointing to newdb
console.log("Switched to "+db.databaseName+" database");
// create 'users' collection in newdb database
db.createCollection("users", function(err, result) {
if (err) throw err;
console.log("Collection is created!");
// close the connection to db when you are done with it
db.close();
});
});
Output:
shashank@tutorialkart:~/workspace/nodejs/mongodb$ node script.js
Switched to newdb database
Collection is created!
Conclusion
Finally, at the end of the tutorial, we looked at MongoDB usability and how to use NodeJS to build a collection. Keep following for more excellent as well as informative tutorials.
Happy Learning!