Mongoose Document Model.updateOne() API
Last Updated :
30 Aug, 2022
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 is an object array that contains key-value pairs where keys are the columns/attributes in the document.
- options: It is an object with various properties.
- callback: It is a callback function that will run once execution is completed.
Setting up Node.js application:
Step 1: Create a Node.js application using the following command:
npm init
Step 2: After creating the NodeJS application, Install the required module using the following command:
npm install mongoose
Project Structure: The project structure will look like this:
Database Structure: The database structure will look like this, the following documents are present in the collection.
Example 1: In this example, We have established a database connection using mongoose and defined model over customerSchema, having two columns “name”, and “orderCount”. At the end, we are using updateOne() method on the Customer model which will filter a single document from the collection and update that document In this example, we are filtering documents based on the name field and updating “orderCount” value to 0.
app.js: Write down the below code in the app.js file:
Javascript
const mongoose = require( 'mongoose' );
mongoose.connect(
useNewUrlParser: true ,
useUnifiedTopology: true
})
const customerSchema = new mongoose.Schema(
{ name: String, orderCount: Number }
)
const Customer = mongoose.model( 'Customer' , customerSchema);
Customer.updateOne({ name: 'Rahul' }, { orderCount: 0 })
.then(result => {
console.log(result)
});
|
Steps to run the program: To run the application execute the below command from the root directory of the project:
node app.js
Output:
{
acknowledged: true,
modifiedCount: 1,
upsertedId: null,
upsertedCount: 0,
matchedCount: 1
}
You can use any GUI tool for the representation of the database in graphical form. Here, I have used the Robo3T GUI tool for graphical representation.
Example 2: In this example, we are filtering documents based on the name field and updating the “name” value to “Customer2 Updated”.
app.js: Write down the below code in the app.js file:
Javascript
const mongoose = require( 'mongoose' );
mongoose.connect(
useNewUrlParser: true ,
useUnifiedTopology: true
})
const customerSchema = new mongoose.Schema(
{ name: String, orderCount: Number }
)
const Customer = mongoose.model(
'Customer' , customerSchema);
Customer.updateOne({ name: [ 'Customer2' ] },
{ name: "Customer2 Updated" }).then(result => {
console.log(result)
});
|
Steps to run the program: To run the application execute the below command from the root directory of the project:
node app.js
Output:
{
acknowledged: true,
modifiedCount: 1,
upsertedId: null,
upsertedCount: 0,
matchedCount: 1
}
You can use any GUI tool for the representation of the database in graphical form. Here, I have used the Robo3T GUI tool for graphical representation.
Reference: https://mongoosejs.com/docs/api/model.html#model_Model-updateOne
Share your thoughts in the comments
Please Login to comment...