How does Query.prototype.getUpdate() work in Mongoose?
Last Updated :
17 Mar, 2021
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' );
useNewUrlParser: true ,
useCreateIndex: true ,
useUnifiedTopology: true
});
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()
useNewUrlParser: true ,
useCreateIndex: true ,
useUnifiedTopology: true
});
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
Share your thoughts in the comments
Please Login to comment...