Mongoose Document prototype.getChanges() API
Last Updated :
28 Sep, 2022
The prototype.getChanges() method of the Mongoose API can be used to see the changes performed on the document object of the collection. This method can be used after modifying the document object values if you want to see which fields and attributes have been affected in the database.
Syntax:
doc.getChanges()
The prototype.getChanges() method does not accept any parameter.
Return Value: The prototype.getChanges() function returns an object with the changes that happened to the 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:
Example 1: In this example, We have established a database connection using mongoose and defined model over userSchema, having two columns “name”, and “age”. At the end, we are using getChanges() method two times before updating field values and after updating document field values on the document object of User model. We are getting empty object in case of executing getChanges() before updating document field values, and getting object with properties or fields which have been updated.
- app.js: Write down the below code in the app.js file:
Javascript
const mongoose = require( "mongoose" );
useNewUrlParser: true ,
useUnifiedTopology: true ,
});
const userSchema = new mongoose.Schema({
name: String,
age: Number,
});
const User = mongoose.model( "User" , userSchema);
User.create({ name: "User4" , age: 40 }).then(doc => {
const getChanges1 = doc.getChanges()
console.log(getChanges1)
doc.name = "User4 Updated"
const getChanges2 = doc.getChanges()
console.log(getChanges2)
});
|
Steps to run the program: To run the application execute the below command from the root directory of the project:
node app.js
Output:
{} // before modification
{ '$set': { name: 'User4 Updated' } } //after modification
Example 2: In this example, we are using getChanges() method once after updating “name” and “age” field values on document object of user model. In this example, we have used asynchronous function to achieve the functionality.
- app.js: Write down the below code in the app.js file:
Javascript
const mongoose = require( "mongoose" );
useNewUrlParser: true ,
useUnifiedTopology: true ,
});
const userSchema = new mongoose.Schema({
name: String,
age: Number,
});
const User = mongoose.model( "User" , userSchema);
const getChangesFunction = async () => {
const doc = await User.create({ name: "User5" , age: 50 })
doc.name = "User5 Updated"
doc.age = 500;
doc.save();
const getChanges = doc.getChanges()
console.log(getChanges)
};
getChangesFunction();
|
Steps to run the program: To run the application execute the below command from the root directory of the project:
node app.js
Output:
{ '$set': { name: 'User5 Updated', age: 500 } }
Reference: https://mongoosejs.com/docs/api/document.html#document_Document-getChanges
Share your thoughts in the comments
Please Login to comment...