Open In App

Mongoose Query.prototype.setQuery() API

Last Updated : 11 Jan, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

The Mongoose Query API.prototype.setQuery() method of the Mongoose API is used on the Query objects. It allows us to set and configure the query object in order to return the JSON object. This method accepts the query condition object as a parameter. Let us understand setQuery() method using an example.

Syntax:

query.setQuery( object );

Parameters: This method accepts a single parameter as described below:

  • object: It is used to specify the query condition object.

Return Value: This method does not return any value.

Setting up Node.js Mongoose Module:

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 database present in the MongoDB.

 

Example 1: The below example illustrates the basic functionality of the Mongoose Connection setQuery() method. In this example, we are defining object with name property and value. At the end, using getQuery() we are getting value for name property.

Filename: app.js

Javascript




// Require mongoose module
const mongoose = require("mongoose");
  
// Set Up the Database connection
  
const connectionObject = mongoose.createConnection(URI, {
    useNewUrlParser: true,
    useUnifiedTopology: true,
});
  
const studentSchema = new mongoose.Schema({
    name: { type: String },
    age: { type: Number },
    rollNumber: { type: Number },
});
  
const Student = connectionObject.model('Student', studentSchema);
  
const query = Student.findByIdAndUpdate(
    { _id: '63a4a9a207370cdcd1961b2c' },
    { name: 'Student2' }
);
query.setQuery({ name: 'Student2' });
console.log(query.getQuery('name'));


Step to run the program: To run the application execute the below command from the root directory of the project:

node app.js

Output:

{ name: 'Student2' }

Example 2: The below example illustrates the basic functionality of the Mongoose Connection setQuery() method. In this example, we are defining object with age and _id property and value. At the end, using getQuery() we are getting object with all the properties.

Filename: app.js

Javascript




// Require mongoose module
const mongoose = require("mongoose");
  
// Set Up the Database connection
  
const connectionObject = mongoose.createConnection(URI, {
    useNewUrlParser: true,
    useUnifiedTopology: true,
});
  
const studentSchema = new mongoose.Schema({
    name: { type: String },
    age: { type: Number },
    rollNumber: { type: Number },
});
  
const Student = connectionObject.model('Student', studentSchema);
  
const query = Student.find({ name: 'Student3' });
query.setQuery({ _id: '63a4a98407370cdcd1961b1a', age: 33 });
const result = query.getQuery();
console.log(result);


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: '63a4a98407370cdcd1961b1a', age: 33 }

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



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads