Node.js Tutorial

Node.js MongoDB Insert Record

Node.js MongoDB Insert Record
Menu

Node.js MongoDB Insert Record

When working with Node.js and MongoDB, one of the most common tasks is inserting data into a collection. MongoDB provides simple methods like insertOne() and insertMany() to make this process quick and efficient. Lets explore how to use these methods with practical examples.

Data Science Tutorial:

Insert a Single Record

The insertOne() method is used to insert a single record into a MongoDB collection. The method takes an object as its first argument, where each key-value pair represents a field and its value in the document.

Example: Inserting a single record in the “employees” collection

Create a JavaScript file named insert.js, and add the following code:

var MongoClient = require('mongodb').MongoClient;  
var url = "mongodb://localhost:27017/MongoDatabase";  
MongoClient.connect(url, function(err, db) {  
  if (err) throw err;  
  var myobj = { name: "Ajeet Kumar", age: "28", address: "Delhi" };  
  db.collection("employees").insertOne(myobj, function(err, res) {  
    if (err) throw err;  
    console.log("1 record inserted");  
    db.close();  
  });  
});  

Now, open your terminal and run the following command:

Introduction to Applied AI:

node insert.js

Youll see the message 1 record inserted in the console, which confirms that your record has been successfully added to the MongoDB collection.

Download New Real Time Projects :Click here

Insert Multiple Records

To insert multiple records at once, MongoDB provides the insert() method (or the newer insertMany() method). This method accepts an array of objects, where each object represents a record you want to insert.

Example: Inserting multiple records in the “employees” collection

SQL Tutorial :

Create a JavaScript file named insertall.js, and add the following code:

var MongoClient = require('mongodb').MongoClient;  
var url = "mongodb://localhost:27017/MongoDatabase";  
MongoClient.connect(url, function(err, db) {  
  if (err) throw err;  
  var myobj = [     
    { name: "Mahesh Sharma", age: "25", address: "Ghaziabad"},  
    { name: "Tom Moody", age: "31", address: "CA"},  
    { name: "Zahira Wasim", age: "19", address: "Islamabad"},  
    { name: "Juck Ross", age: "45", address: "London"}  
  ];  
  db.collection("employees").insert(myobj, function(err, res) {  
    if (err) throw err;  
    console.log("Number of records inserted: " + res.insertedCount);  
    db.close();  
  });  
});  

Run the script using the following command:

Deep Learning Tutorial: 

node insertall.js

After running this command, youll see an output indicating that multiple records have been successfully inserted into the collection.

Machine Learning Tutorial:
Complete Advance AI topics:- 
Complete Python Course with Advance topics:-


how to insert data in mongodb using node js using mongoose mongodb insertone example how to insert data in mongodb compass mongodb insert document example search in node js mongodb mongodb insert one insertone mongodb nodejs insert multiple documents in mongodb compass node js mongodb insert record example node js mongodb insert record json mongodb insert using nodejs, how to insert data to mongodb using nodejs, how to insert thousands + records in mongodb, how i insert a data in mongodb node.js?, bulk insert in node js mongodb, insert into mongodb node js, insert data mongodb node js, node js insert data in mongodb, bulk insert in mongodb node js, nodejs mongodb api, mongodb nodejs, nodejs mongodb project, nodejs mongodb rest api, how to insert data to mongodb using nodejs and expressjs, nodejs mongodb crud, nodejs express mongodb crud

Source Code Available

Interested in This Project?

Get the complete source code for this project at a very affordable price — perfect for your portfolio, college submission, or learning. Message us on WhatsApp and we'll get back to you instantly!

Full source code included Step-by-step setup guide Instant delivery on WhatsApp Instant reply on WhatsApp
Chat on WhatsApp

We usually reply within a few minutes

Leave a Reply

Your email address will not be published. Required fields are marked *

Chat with us