Node.js MongoDB Create Collection
MongoDB stores data in collections, which are similar to tables in traditional databases. When working with MongoDB in Node.js, you can create a new collection using the official MongoDB driver and the createCollection() method.
In this tutorial, we will learn how to create a MongoDB collection using Node.js, install the MongoDB driver, and use the modern async/await approach with practical examples.
Table of Contents
Install the MongoDB Driver
To connect a Node.js application with MongoDB, first install the official MongoDB driver using npm:
More:- UPDATEGADH
npm install mongodb
Node.js MongoDB Create Collection Example
In this example, we will create an employees collection inside the MongoDatabase database.
// employees.js
const { MongoClient } = require('mongodb');
const url = "mongodb://localhost:27017";
const dbName = "MongoDatabase";
MongoClient.connect(url, function(err, client) {
if (err) throw err;
const db = client.db(dbName);
db.createCollection("employees", function(err) {
if (err) throw err;
console.log("Collection is created!");
client.close();
});
});
How the Example Works
MongoClientis imported from the MongoDB driver.mongodb://localhost:27017is the connection URL for the local MongoDB server.client.db()selects the database.createCollection("employees")creates the employees collection.client.close()closes the MongoDB connection after the operation is completed.
Run the Node.js Script
YT:- DecodeIT
Save the code in a file named employees.js and run the following command in your terminal:
node employees.js
If the collection is created successfully, you will see the following output:
Collection is created!
Modern Async/Await Version
Modern Node.js applications commonly use async/await because it makes asynchronous code easier to read and maintain.
const { MongoClient } = require('mongodb');
async function run() {
const client = new MongoClient("mongodb://localhost:27017");
await client.connect();
const db = client.db("MongoDatabase");
await db.createCollection("employees");
console.log("Collection created!");
await client.close();
}
run().catch(console.error);
Why Use Async/Await?
The async/await syntax provides a clean way to handle asynchronous MongoDB operations. It also makes the code easier to understand compared to older callback-based approaches.
MongoDB Automatically Creates Collections
You do not always need to explicitly call createCollection(). MongoDB can automatically create a collection when you insert the first document into a collection that does not already exist.
await db.collection("employees").insertOne({
name: "Rahul",
department: "IT"
});
For simple applications, explicit collection creation may therefore not be necessary. The createCollection() method is useful when you need to create a collection with specific options or configuration.
MongoDB Create Collection: Key Points
| Method | Purpose |
|---|---|
MongoClient | Establishes a connection with the MongoDB server. |
client.db() | Selects a specific database. |
createCollection() | Explicitly creates a new collection. |
insertOne() | Inserting the first document can automatically create the collection. |
Conclusion
Creating a MongoDB collection in Node.js is simple with the official MongoDB driver. The createCollection() method allows you to explicitly create a collection, while the modern async/await approach makes MongoDB operations cleaner and easier to manage.
Keywords: mongodb create collection example, how to create collection in mongodb using node js mongoose, mongodb create collection if not exists, create collection mongodb shell, mongodb create collection with index, how to create collection in mongodb atlas, node js mongodb create collection example, nodejs mongodb express Node.js MongoDB Create Collection Node.js MongoDB create collection example create collection in MongoDB using Node.js MongoDB create collection Node.js Node.js MongoDB tutorial MongoDB collection Node.js createCollection MongoDB Node.js MongoDB Node.js driver MongoDB createCollection() example Node.js MongoDB database tutorial how to create collection in MongoDB using Node.js MongoDB collection creation Node.js MongoDB async await MongoDB create collection with Node.js