![]() |
MongoDB, a popular NoSQL database, offers flexibility and scalability for handling data. If you’re developing a Node.js application and need to interact with MongoDB, one of the fundamental operations you’ll perform is inserting a document into a collection. This article provides a step-by-step guide on how to accomplish this using Node.js. Prerequisites:The steps to insert documents in MongoDB collection are given below Table of Content Steps to Setup the ProjectStep 1: Create a nodeJS application by using this command npm init
Step 2: Install the necessary packages/libraries in your project using the following commands. npm install mongodb Project Structure:![]() Project Structure The updated dependencies in package.json file will look like: "dependencies": { NodeJS and MongoDB ConnectionOnce the MongoDB is installed we can use MongoDB database with the Nodejs Project.Initially we need to specify the database name ,connection URL and the instance of MongoDBClient. const { MongoClient } = require('mongodb');
Create a Collection in MongoDb using Node JsIn this operation we create a collection inside a database.Intilally we specify the database in which collections is to be created. //Sepcify Database
Insert a Single DocumentTo insert a document into the collection insertOne() method is used. const insertDoc = await collection.insertOne({ Insert Many DocumentTo insert a document into the collection insertMany() method is used. const doc_array = [ Handling Insertion ResultsIn a project we have different tasks which needs to be executed in specific order.In the MongoDB and Node Js project we must ensure that connection is set.While performing insertion of documents , we perform asynchronous insertion so that execution is not interrupted.We use try-catch block to handle errors while setting up connection, inserting document or while performing any other operation. If an error occurs during execution ,catch block handles it or provide the details about the error ,which helps to resolve the error. try {
Read Documents from the collectionWe can read the documents inside the collection using the find() method. const doc = await collection.find({}).toArray(); find() method is used to along with empty {} are used to read all the documents in the collection.Which are further converted into the array using the toArray() method. Closing the Connectionfinally{
Example: Implementation to show Insertion of documents into a MongoDB collection using Node.js
Output: ![]() Insert Document in MongoDB Explanation : In the above example, Initially MongoClient class is imported which is used to connect MongoDB and Nodejs .client is the instance of MongoDb and Node Js connection. which is used to name the database .As database is set ,collection() method sets the instance of the collection .Three documents are inserted in the students collection using insertMany() method .Error during the execution are handled using the try catch block ,finally connection is closed using the close() method ConclusionInserting a document into a MongoDB collection using Node.js involves setting up a MongoDB client, connecting to the database, and using the appropriate methods to perform the insert operation. This guide has covered the basics, but MongoDB’s Node.js driver offers many more features for advanced database interactions. For more information, check out the official MongoDB Node.js Driver documentation. |
Reffered: https://www.geeksforgeeks.org
MongoDB |
Type: | Geek |
Category: | Coding |
Sub Category: | Tutorial |
Uploaded by: | Admin |
Views: | 20 |