Mongoose Document prototype.overwrite() API
Last Updated :
28 Sep, 2022
The API prototype.overwrite() method of the Mongoose API is used to overwrite the existing document object in the collection. It is used to update or replace all the existing values in the document with the values passed in the object as a parameter, except for immutable properties.
Syntax:
doc.overwrite()
Parameters: The prototype.overwrite() method accepts three parameters:
- doc: It is an object which will overwrite existing document objects.
Returns: The prototype.overwrite() function returns the updated document.
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 userSchema, having two columns or fields “name” and “age”. In the end, we are using overwrite() method on the object of the User model which will overwrite the existing document values with the value we are providing to this method as a first parameter.
- app.js: Write down the below code in the app.js file:
App.js
const mongoose = require( "mongoose" );
mongoose.connect( "
useNewUrlParser: true ,
useUnifiedTopology: true ,
});
const userSchema = new mongoose.Schema({
name: String,
age: Number,
});
const User = mongoose.model( "User" , userSchema);
User.findById( "6320e0379cdf11e37f2b0b23" ).then(newDoc => {
const output = (newDoc).overwrite({
name: "User5 Updated" , age: 500 });
console.log(output)
})
|
Step to run the program: To run the application execute the below command from the root directory of the project:
node app.js
Output:
{
_id: new ObjectId("6320e0379cdf11e37f2b0b23"),
name: 'User5 Updated',
age: 500,
__v: 0
}
Example 2: In this example, we are overwriting the field values of an existing document and then using save() on the document object to persist the latest values in the database.
- app.js: Write down the below code in the app.js file:
App.js
const mongoose = require( "mongoose" );
mongoose.connect( "
useNewUrlParser: true ,
useUnifiedTopology: true ,
});
const userSchema = new mongoose.Schema({
name: String,
age: Number,
});
const User = mongoose.model( "User" , userSchema);
const overWriteDoc = async () => {
const doc = User.findById( "63203694182cd3c22ea480ff" )
const output = (await doc).overwrite({
name: "User1 Updated" , age: 100 });
output.save();
console.log(output)
}
overWriteDoc();
|
Step to run the program: To run the application execute the below command from the root directory of the project:
node app.js
Output:
{
_id: new ObjectId("63203694182cd3c22ea480ff"),
name: 'User1 Updated',
age: 100,
__v: 0
}
Reference: https://mongoosejs.com/docs/api/document.html#document_Document-overwrite
Share your thoughts in the comments
Please Login to comment...