Open In App

Mongoose updateOne() Function

Last Updated : 15 Jan, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

The updateOne() function is used to update the first document that matches the condition. This function is the same as update(), except it does not support the multi or overwrite options.

Syntax:

Model.updateOne(id)

Parameters:

  • Model: This is the name of the collection used to retrieve the document corresponding to the provided ID.
  • id: This is the identifier for the document you intend to locate.
  • callback`updateOne` also accepts a callback function, which can manage errors or perform actions with the retrieved document.

Steps to Installation of Mongoose Module:

Step 1: You can install this package by using this command.

npm install mongoose

Step 2: After installing the mongoose module, you can check your mongoose version in the command prompt using the command.

npm version mongoose

Project Structure:

NodeProj

The updated dependencies in package.json file will look like:

"dependencies": {
"mongoose": "^7.6.5",
}

Example: Below the code example for the updateOne() method:

javascript




const mongoose = require('mongoose');
 
// Database connection
    useNewUrlParser: true,
    useCreateIndex: true,
    useUnifiedTopology: true,
    useFindAndModify: false
});
 
// User model
const User = mongoose.model('User', {
    name: { type: String },
    age: { type: Number }
});
 
// Find all documents matching the condition
// (age >= 5) and update first document
// This function has 4 parameters i.e.
// filter, update, options, callback
User.updateOne({age:{$gte:5}},
    {name:"ABCD"}, function (err, docs) {
    if (err){
        console.log(err)
    }
    else{
        console.log("Updated Docs : ", docs);
    }
});


Steps to run the program:

node index.js

Below is the sample data in the database before the function is executed, You can use any GUI tool or terminal to see the database, like we have used Robo3T GUI tool as shown below: Database

Console Output:

After the function is executed, You can see the database as shown below: new Database

So this is how you can use the mongoose updateOne() function which is used to update the first document that matches the condition. This function is the same as the update(), except it does not support the multi or overwrite options.

We have a mongoose queries complete reference where we covered all the mongoose queries to check those please go through Mongoose Queries article.



Similar Reads

Mongoose Queries Model.updateOne() Function
The Queries Model.updateOne() function of the Mongoose API is used to update an existing document with the information mentioned in the "update" object. It updates only the first document that is returned in the filter. Syntax: Model.updateOne(filter, update, options, callback ) Parameters: It accepts the following 4 parameters as mentioned above a
3 min read
Mongoose Document Model.updateOne() API
The Model.updateOne() method of the Mongoose API is used to update documents in a collection. This method will update the very first document that will match the filter irrespective of the value of the multi-option. Model.updateOne() method accepts four parameters: filter: It is an object to filter out a document that needs to be updated.update: It
3 min read
Mongoose Document prototype.updateOne() API
The API prototype.updateOne() of the Mongoose API is used to update documents in a collection. This method can be used on mongoose documents and on that document you can update various fields in one command. Syntax: doc.updateOne() Parameters: The prototype.updateOne() method accepts three parameters: doc: It is an object with fields and values to
3 min read
Mongoose Query.prototype.updateOne() API
The Mongoose Query API.prototype.updateOne() method of the Mongoose API is used on the Query objects. It allows us to update the document in the collection. MongoDB updates the first document that matches the filter regardless of the multi-option value. Let us understand updateOne() method using an example. Syntax: Model.updateOne( filter, update,
3 min read
MongoDB - updateOne() Method
The updateOne() method in MongoDB updates the first matched document within the collection based on the given query. The value of the _id field remains unchanged after updating the value. This method updates one document at a time and can also add new fields to the given document. Important Points:This method can accept a document that only holds u
4 min read
Mongoose mongoose.model() Function
The mongoose.model() function of the mongoose module is used to create a collection of a particular database of MongoDB. The name of the collection created by the model function is always in plural format mean GFG to gfss and the created collection imposed a definite structure. Syntax: mongoose.model(<Collectionname>, <CollectionSchema>
2 min read
Mongoose | updateMany() Function
The updateMany() function is same as update(), except MongoDB will update all documents that match the filter. It is used when the user wants to update all documents according to the condition. Installation of mongoose module: You can visit the link to Install mongoose module. You can install this package by using this command. npm install mongoose
2 min read
Mongoose | findOneAndDelete() Function
The findOneAndDelete() function is used to find a matching document, remove it, and passes the found document (if any) to the callback. Installation of mongoose module: You can visit the link to Install mongoose module. You can install this package by using this command. npm install mongoose After installing mongoose module, you can check your mong
2 min read
Mongoose | deleteOne() Function
The deleteOne() function is used to delete the first document that matches the conditions from the collection. It behaves like the remove() function but deletes at most one document regardless of the single option. Installation of mongoose module: You can visit the link to Install mongoose module. You can install this package by using this command.
2 min read
Mongoose | findByIdAndDelete() Function
The findByIdAndDelete() function is used to find a matching document, removes it, and passing the found document (if any) to the callback. Installation of mongoose module: You can visit the link to Install mongoose module. You can install this package by using this command. npm install mongoose After installing mongoose module, you can check your m
2 min read