How to update record without objectID in mongoose?
Last Updated :
28 Apr, 2025
Mongoose is an ODM(Object Data Library) for MongoDB in Node JS that helps to write schema, validation and business logic in a simple way without the hassle of native MongoDB boilerplate.
Prerequisites
Approach to update record without objectID:
There are two ways we can update a record in MongoDB using Mongoose
- Using FindOneandUpdate() method
- Using FindOne and FindByIdandUpdate Method
Steps to Create Node Application and Installation of mongoose module:
Step 1: Open the terminal in the folder you want and enter the following command:
npm init -y
Step 2: Your node project will be ready. create the index.js file using the command below
touch index.js
Step 3: You can install this package by using this command.
npm install mongoose
Step 4: Connection to MongoDB using mongoose:
In the index.js file import the mongoose module and use the connect function to connect the database
mongoose.connect('mongodb://127.0.0.1:27017/GFG').then(console.log('Connected to MongoDB')
Here is the connection string where i have used 127.0.0.1 you can also use localhost, next is the port no which is 27017 and then is the name of the database where you want to create your collections
Folder Structure:
Folder structure
The updated dependencies in package.json file will look like:
"dependencies": {
"mongoose": "^8.0.3"
}
Approach 1: Using FindOneAndUpdate Methods to update the records
Example: Add this code in index.js file and insert json file in the MongoDB collection named Users.
JavaScript
//index.js
const mongoose = require('mongoose');
const setup = async () => {
await mongoose.connect('mongodb://127.0.0.1:27017/GFG')
.then(console.log('Connected to MongoDB')).catch(error =>
console.error('Failed to connect to MongoDB:', error));
// Define the schema
const userSchema = new mongoose.Schema({
email: { type: String, unique: true },
name: String,
age: Number,
});
// Create a model based on the schema
const User = mongoose.model('User', userSchema);
// Find a user and update their age
const findUserAndUpdateAge = async () => {
try {
// Use await to get the result of findOneAndUpdate
const user = await User.findOneAndUpdate({ email: '[email protected]' },
{ age: 30 },
{ new: true }
);
console.log(user, "User Updated");
} catch (err) {
console.log(err);
}
};
// Call the update function
await findUserAndUpdateAge();
};
// Call the setup function to execute the code
setup();
JavaScript
[{
"_id": {
"$oid": "65767ccc6952cf734319b873"
},
"email": "[email protected]",
"name": "John Doe",
"age": 25,
"__v": 0
},
{
"_id": {
"$oid": "65767cea46c027123509beef"
},
"email": "[email protected]",
"name": "John Doe",
"age": 25,
"__v": 0
},
{
"_id": {
"$oid": "65767cf53ad13463a619c8c9"
},
"email": "[email protected]",
"name": "John Doe",
"age": 25,
"__v": 0
}]
MongoDB inserted data:

Steps to run the project: Open the terminal and write the following command
node index.js
Output:

Approach 2: Using FindOne and FindByIdandUpdate Method to update the records
JavaScript
//index.js
const mongoose = require('mongoose');
const setup = async () => {
await mongoose.connect('mongodb://127.0.0.1:27017/GFG').then(console.log('Connected to MongoDB')).catch(error =>
console.error('Failed to connect to MongoDB:', error));
// Define the schema
const userSchema = new mongoose.Schema({
email: { type: String, unique: true },
name: String,
age: Number,
});
// Create a model based on the schema
const User = mongoose.model('User', userSchema);
// Find a user and update their age
const findUserAndUpdateAge = async () => {
try {
// Use await to get the result of findOneAndUpdate
const user = await User.findOne({ email: '[email protected]' });
const updatedUser = await User.findByIdAndUpdate(user._id, { age: 30 }, { new: true });
console.log(updatedUser, "User Updated");
} catch (err) {
console.log(err);
}
};
// Call the update function
await findUserAndUpdateAge();
};
// Call the setup function to execute the code
setup();
Step to run the application: To run this file you need to run the following command.
node index.js
Output:

Similar Reads
How to Partially Updating Objects in MongoDB Updating documents in MongoDB is a common operation in database management. Sometimes, we may only want to update specific fields of a document without replacing the entire object. MongoDB provides powerful mechanisms to achieve this which allows us to merge new data with existing documents seamless
4 min read
How to Use findOneAndUpdate() in Mongoose? In the world of MongoDB and Node.js development, Mongoose serves as a powerful tool for interacting with MongoDB databases. One of Mongoose's key methods, findOneAndUpdate, allows developers to easily find a single document in a MongoDB collection, update it, and optionally return either the origina
5 min read
How to Use Mongoose Without Defining a Schema? Mongoose is a powerful and flexible Node.js library that simplifies interactions with MongoDB. Typically, when working with Mongoose, you define a schema to structure your data before interacting with the database. when using Mongoose, developers define a schema to structure their data, ensuring con
4 min read
How to Update the First Object in an Array in MongoDB MongoDB, a popular NoSQL database, offers powerful features for handling complex data structures. One common scenario is updating specific elements within arrays stored in documents. In this guide, we'll focus on updating the first object in an array within a MongoDB document. We'll cover the concep
3 min read
How to Converting ObjectId to String in MongoDB In MongoDB, documents are uniquely identified by a field called ObjectId. While ObjectId is a unique identifier for each document there may be scenarios where we need to convert it to a string format for specific operations or data manipulation. In this article, we'll learn about the process of conv
4 min read