Open In App

Mongoose Document prototype.overwrite() API

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:

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.




// Require mongoose module
const mongoose = require("mongoose");
  
// Set Up the Database connection
mongoose.connect("
    useNewUrlParser: true,
    useUnifiedTopology: true,
});
  
const userSchema = new mongoose.Schema({
    name: String,
    age: Number,
});
  
// Defining userSchema model
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.




// Require mongoose module
const mongoose = require("mongoose");
  
// Set Up the Database connection
mongoose.connect("
    useNewUrlParser: true,
    useUnifiedTopology: true,
});
  
const userSchema = new mongoose.Schema({
    name: String,
    age: Number,
});
  
// Defining userSchema model
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


Article Tags :