Open In App

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

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

The Query.prototype.comment() is used to specify the comment options. It makes possible to attach a comment to a query in any context that $query may appear.
Syntax:

Query.prototype.comment()

Parameters: This function has one array parameter i.e. comment of string type.
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

Now, create a folder and add a file for example, index.js as shown below.

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 }
});
  
var query = User.find();
query.comment('Query to find all documents')
  
console.log("Comment: ", query.options)


Steps to run the program: 
 

The project structure will look like this: 
 

Run index.js file using below command: 

node index.js

Output: 

Comment:  { comment: 'Query to find all documents' }

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 }
});
  
var query = User.find();
query.comment('Find All')
  
console.log("Comment: ", query.options);
  
app.listen(3000, function(error ) {
    if(error) console.log(error)
    console.log("Server listening on PORT 3000")
});


Steps to run the program:

The project structure will look like this:

Run index.js file using below command: 

node index.js

Output: 

Server listening on PORT 3000
Comment:  { comment: 'Find All' }

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



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads