Mongoose Query prototype.where() API
Mongoose is a MongoDB object modeling and handling for a node.js environment. Mongoose Query where() method is used to filter the documents from a collection using the MongoDB query system. It can be used in various ways, like chaining or passing query conditions. Let’s understand more about this with some examples.
Syntax:
Query.prototype.where()
Parameters: It accepts 2 parameters that are described below::
- path: It is of type String or Object.
- val: It can be of any type.
Return type: It returns the Query object.
Creating node application And Installing Mongoose:
Step 1: Create a node application using the following command:
mkdir folder_name cd folder_name npm init -y touch main.js
Step 2: After creating the ReactJS application, Install the required module using the following command:
npm install mongoose
Project Structure: It will look like the following.
GUI Representation of the Database using MongoDB Compass: Currently, the collection has no data.
Example 1: In this example, we will use Query where() method to filter out the documents using the chaining method where we would chain different queries together.
Filename: main.js
Javascript
const mongoose = require( 'mongoose' ) // Database connection mongoose.connect( 'mongodb://localhost:27017/query-helpers' , { dbName: 'event_db' , useNewUrlParser: true , useUnifiedTopology: true }, err => err ? console.log(err) : console.log( 'Connected to database' )); const personSchema = new mongoose.Schema({ name: { type: String, }, age: { type: Number, } }); const personsArray = [ { name: 'John' , age: 22 }, { name: 'Dave' , age: 17 }, { name: 'Earl' , age: 12 } ] const Person = mongoose.model( 'Person' , personSchema); (async () => { await Person.insertMany(personsArray); const persons = await Person.find().where( 'age' ).gte(18); console.log(persons); })() |
Step to Run Application: Run the application using the following command from the root directory of the project:
node main.js
Output: We see that the value remains unchanged in the result.
GUI Representation of the Database using MongoDB Compass:
Example 2: In this example, we will use Query API where() method to filter out the documents by passing query conditions as the argument to the where() method.
Filename: main.js
Javascript
const mongoose = require( 'mongoose' ) // Database connection mongoose.connect( 'mongodb://localhost:27017/query-helpers' , { dbName: 'event_db' , useNewUrlParser: true , useUnifiedTopology: true }, err => err ? console.log(err) : console.log( 'Connected to database' )); const personSchema = new mongoose.Schema({ name: { type: String, }, age: { type: Number, } }); const personsArray = [ { name: 'John' , age: 22 }, { name: 'Dave' , age: 17 }, { name: 'Earl' , age: 12 } ] const Person = mongoose.model( 'Person' , personSchema); (async () => { await Person.insertMany(personsArray); const persons = await Person.find().where ({ age: { $lte: 18 } }) console.log(persons); })() |
Step to Run Application: Run the application using the following command from the root directory of the project:
node main.js
Output: We see that the value remains unchanged in the result.
GUI Representation of the Database using MongoDB Compass:
Reference: https://mongoosejs.com/docs/api/query.html#query_Query-where
Contact Us