Node.js – Create Collection in MongoDB

We can create a collection in MongoDB database from Node.js program.

In this Node.js Tutorial, we shall learn to Create Collection in MongoDB Database from Node.js Application, using db.createCollection() method, with an example.

Steps to Create Collection in MongoDB via Node.js

Following is a step by step guide with an example to create a collection in MongoDB from Node.js Application.

Step 1: Start MongoDB Service.

Run the following command to start MongoDB Service.

sudo service mongod start

Step 2: Get the base URL to MongoDB Service.

A simple hack to know the base url of MongoDB Service is to Open a Terminal and run Mongo Shell.

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

While the Mongo Shell starts up, it echoes back the base url of MongoDB.


Step 3: Prepare the complete URL.

Append the Database name you want to connect to (say newdb), to the base URL.


Step 4: Create a MongoClient.

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

Step 5: Make connection from MongoClient to the MongoDB Server with the help of URL.

MongoClient.connect(url, <callback_function>);

Once the MongoClient is done trying to make a connection, the callback function receives error and db object as arguments.

If the connection is successful, the db object points to the database, newdb.

Step 6: Create a MongoDB Collection in the database.

Following is the syntax of createCollection() method used to create collection in MongoDB from Node.js.

db.createCollection(<collection_name>, <callback_function>)
collection_nameName of the new MongoDB Collection, that we would like to create
callback_functionThis Node.js Callback Function is called after Node has tried creating a collection, and ready with the result. The callback function receives error and result object as arguments.

Example 1 – Create Collection in MongoDB via Node.js

In this example, we will connect to a MongoDB Database and create a collection named “users”.


// 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


arjun@tutorialkart:~/workspace/nodejs/mongodb$ node node-js-mongodb-create-collection.js 
Switched to newdb database
Collection is created!


MongoDB Tutorial – Learn MongoDB from basics with Examples.


In this Node.js MongoDB tutorial: Node.js – Create Collection in MongoDB, we have learnt to create a collection in a database from Node.js Application using mongodb package. In our next tutorial – Node.js MongoDB Delete Collection, we shall learn to delete the collection, or you may skip to Node.js Insert Document(s) to MongoDB Collection.