How to use insertOne() Method with insertedId Property In NodeJS
In the MongoDB, insertOne() method is used to insert a single document into the collection. It also returns the document that contains inserted document details & inserted Id property.
Syntax:
db.collection.insertOne(document, callback)
- document: The document to be inserted into the collection.
- callback: An optional callback function that is called after the insertion is complete
Example 1: Adding the Java course and getting the Inserted Id
const mongoose = require("mongoose");
const Course = require("./model");
const db = require("./dbConnect");
async function insertData() {
try {
const javaCourse = new Course({
Title: "Java",
Fees: 10000,
Instructor: "Akhil Sir",
date: new Date("2024-03-23"),
});
const savedCourse = await javaCourse.save();
const insertedId = savedCourse._id;
console.log("Inserted document _id:", insertedId);
console.log("Data inserted successfully");
const insertedData = await Course.find({});
console.log("Inserted data:");
console.log(insertedData);
} catch (error) {
console.error("Error inserting data:", error);
} finally {
mongoose.disconnect();
}
}
insertData();
Output:
Server is running on port 3000
MongoDB connected
Inserted document _id: new ObjectId('662894fbef2273c58f5d1592')
Data inserted successfully
Inserted data:
[
{
_id: new ObjectId('662894fbef2273c58f5d1592'),
Title: 'Python',
Fees: 25000,
Instructor: 'Sushant Sir',
date: 2024-03-23T00:00:00.000Z,
__v: 0
}
]
Example 2: Adding the Python course & getting the Inserted Id
const mongoose = require("mongoose");
const Course = require("./model");
const db = require("./dbConnect");
async function insertData() {
try {
const pythonCourse = new Course({
Title: "Python",
Fees: 25000,
Instructor: "Sushant Sir",
date: new Date("2024-03-23"),
});
const savedCourse = await pythonCourse.save();
const insertedId = savedCourse._id;
console.log("Inserted document _id:", insertedId);
console.log("Data inserted successfully");
const insertedData = await Course.find({});
console.log("Inserted data:");
console.log(insertedData);
} catch (error) {
console.error("Error inserting data:", error);
} finally {
mongoose.disconnect();
}
}
insertData();
Output:
Server is running on port 3000
MongoDB connected
Inserted document _id: new ObjectId('662894c857ef020ca85343f0')
Data inserted successfully
Inserted data:
[
{
_id: new ObjectId('662894c857ef020ca85343f0'),
Title: 'Python',
Fees: 25000,
Instructor: 'Sushant Sir',
date: 2024-03-23T00:00:00.000Z,
__v: 0
}
]
How to Get the id of Inserted Document in MongoDB in NodeJS
MongoDB is a popular type of NoSQL database. It stores data in documents that look like JSON. When working with MongoDB using Mongoose, _id is a unique identifier of each document in a collection and it can be used to perform operations on the document, such as updating or deleting it. The insertion method is used to insert a document into a MongoDB collection.
In this article, We will learn about the process of Getting the _id of the inserted document in Mongo database in NodeJS by understanding the various methods along with the examples and so on.
Contact Us