Mongoose Query() API
The Mongoose API Query() method of the Mongoose API is used as constructor to create reference of Query. It allows us to create query objects and build query expressions. We do not need to create an object of Query class in order to interact with the mongodb collection. We can directly call any model method on custom defined Model which will indirectly returns the instance of Query. Let us understand the Query() constructor using an example.
Syntax:
new mongoose.Query( options, model, conditions, collection );
Parameters: This method accepts four parameters as described below:
- options: It is used to specify various properties for the constructor.
- model: It is used to specify the model name.
- conditions: It is used to specify the filter conditions.
- collection: It is used to specify the name of the collection in the database.
Return Value: This constructor returns reference of the Query class.
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 Query() constructor. We can see that query object is the instance of Query class, which is indirectly instantiated while using find() on mongoose model.
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, required: true }, age: Number, rollNumber: { type: Number, required: true }, }); const StudentModel = connectionObject.model( 'Student' , studentSchema); const query = StudentModel.find(); console.log(query instanceof mongoose.Query); |
Step to run the program: To run the application execute the below command from the root directory of the project:
node app.js
Output:
true
Example 2: The below example illustrates the basic functionality of the Mongoose Connection Query() constructor. At the end, we are using query object which is the instance of Query constructor in order to get the documents from the collection.
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, required: true }, age: Number, rollNumber: { type: Number, required: true }, }); const StudentModel = connectionObject.model( 'Student' , studentSchema); const query = StudentModel.find(); query.where( 'name' , "Student2" ); query.exec((error, result) => { if (error) { console.log( "Error -" , error); } else { console.log( "Result -" , 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:
Result - [ { _id: new ObjectId("63c2fe2ef9e908eb17f225db"), name: 'Student2', age: 18, rollNumber: 65, __v: 0 } ]
Reference: https://mongoosejs.com/docs/api/query.html#query_Query
Please Login to comment...