Open In App

How does Query.prototype.getUpdate() work in Mongoose?

Last Updated : 17 Mar, 2021
Improve
Improve
Like Article
Like
Save
Share
Report

The Query.prototype.getUpdate() function is used to return the current update operations as a JSON object. It is easy to use and user can easily get the current update operations.

Syntax:  

Query.prototype.getUpdate()

Parameters: This function has no parameter.
Return Value: This function returns Query Object.
Installing mongoose : 

npm install mongoose

After installing the mongoose module, you can check your mongoose version in command prompt using the command. 

npm mongoose --version

After that, you can just create a folder and add a file for example, index.js as shown below.

Database: The sample database used here is shown below:

Project Structure: The project structure will look like this: 
 

Example 1:

index.js




const mongoose = require('mongoose');
  
// Database connection
    useNewUrlParser: true,
    useCreateIndex: true,
    useUnifiedTopology: true
});
  
// User model
const User = mongoose.model('User', { 
    name: { type: String },
    age: { type: Number }
});
  
const query = User.updateOne({name:"Gourav"}, 
                             { $set: { name: 'Geek' } });
console.log(query.getUpdate()); 


Run index.js file using below command: 

node index.js

Output: 

{ '$set': { name: 'Geek' } }

Example 2:

index.js




const express = require('express');
const mongoose = require('mongoose');
const app = express()
  
// Database connection
    useNewUrlParser: true,
    useCreateIndex: true,
    useUnifiedTopology: true
});
  
// User model
const User = mongoose.model('User', { 
    name: { type: String },
    age: { type: Number }
});
  
const query = User.updateOne({name:"Gourav"}, 
                             { $set: { name: 'NewGeek' } });
console.log(query.getUpdate()); 
  
app.listen(3000, function(error ) {
    if(error) console.log(error)
    console.log("Server listening on PORT 3000")
});


Run index.js file using below command: 

node index.js

Output: 

Server listening on PORT 3000
{ '$set': { name: 'NewGeek' } }

Reference: 
https://mongoosejs.com/docs/api/query.html#query_Query-getUpdate
 



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads