Mongoose Document Model.replaceOne() API
Last Updated :
31 Aug, 2022
The Model.replaceOne() method of the Mongoose API is used to replace any one document in a collection. This method works the same as the update method but it replaces MongoDB’s existing document with the given document with any atomic operator i.e $set.
Syntax:
Model.replaceOne()
Parameters: The Model.replaceOne() method accepts four parameters:
- filter: It is an object to filter all the documents with the matching field that needs to be updated.
- doc: It is an object with a field that will replace the existing field.
- options: It is an object with various properties.
- callback: It is a callback function that will run once execution is completed.
Return type: The Model.replaceOne() function returns a promise. The result contains an object with the following key and values or properties.
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 three columns or fields “name”, “orderCount”, and “superUser”. In the end, we are using replaceOne() method on the Customer model which will replace the document based on the condition given as a first parameter to the method. And replace that documents with the fields given in the object as a second parameter to the method. In this example, we are filtering documents based on “_id” and replacing its values with fields provided in an object as a second parameter.
- 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, superUser: Boolean }
)
const Customer = mongoose.model( 'Customer' , customerSchema);
Customer.replaceOne({ _id: '630ca42f5e5d4f1de37cf654' },
{ name: 'Customer3' , orderCount: 100, superUser: true })
.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
}
GUI Representation of the Database using Robo3T GUI tool
Example 2: In this example, we are filtering documents based on “_id” and replacing its values with fields provided in the object as a second parameter. If you see in the GUI output we replaced the “name” field from “Customer2” to “Customer2 Example 2” and also modified values for “orderCount”, this functionality we achieved by using any atomic operator i.e $set.
- 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, superUser: Boolean }
)
const Customer = mongoose.model( 'Customer' , customerSchema);
Customer.replaceOne({ _id: '630ca4260edd88312bf28db1' },
{
name: 'Customer2 Example2' ,
orderCount: 0, superUser: false
}).
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
}
GUI Representation of the Database using Robo3T GUI tool
Reference: https://mongoosejs.com/docs/api/model.html#model_Model-replaceOne