Mongoose Query prototype.all() API
Mongoose is an Object Data Modeling (ODM) library for MongoDB. It defines a strongly-typed schema, with default values and schema validations which are later mapped to a MongoDB document.
The all( ) method of mongoose Query API is used to find the documents which contain a field that has some specific values. The all() method is used on a field whose type is an Array.
Syntax:
Modal.find().all(path, val)
Query.prototype.all() method accepts two parameters:
- path: path is a string that represents the field name, this is a must-have parameter.
- val: val is an array that contains the elements to be searched.
Installation of mongoose module:
Step 1: You can visit the link to Install the mongoose module. You can install this package by using this command.
npm install mongoose
Step 2: After installing the mongoose module, you can check your mongoose version in the command prompt using the command.
npm version mongoose
Step 3: After that, you can just create a folder and add a file for example index.js, To run this file you need to run the following command.
node index.js
Project Structure: The project structure will look like this:

Example 1: We have some data on customers that contains their names and interests. In this example, we want to find customers whose interests are tennis and shooting.
Filename: index.js
Javascript
// Require mongoose module const mongoose = require( 'mongoose' ); // Set Up the Database connection mongoose.connect( useNewUrlParser: true , useUnifiedTopology: true }) // Defining customerSchema schema const customerSchema = new mongoose.Schema( { name: String, interest: Array, orderCount: Number } ) // Defining customerSchema model const Customer = mongoose.model( 'Customer' , customerSchema); // Find all the customers whose interest are tennis and shooting Customer.find().all( "interest" , [ "tennis" , "shooting" ]).then((res) => { console.log(res) }) |
Steps to run the application: Run the index.js file using the below command:
Step 1: Make sure you have installed the mongoose module using the following command:
npm install mongoose
Step 2: Below is the sample data in the database before the function is executed, You can use any GUI tool or terminal to see the database like we have used the MongoDB compass GUI tool as shown below:

Step 3: Run the index.js file using the below command:
node index.js
Output:

Example 2: In this example, we have the same database but instead of passing a second parameter, we are leaving it blank this time.
Javascript
// Require mongoose module const mongoose = require( 'mongoose' ); // Set Up the Database connection mongoose.connect( useNewUrlParser: true , useUnifiedTopology: true }) // Defining customerSchema schema const customerSchema = new mongoose.Schema( { name: String, interest: Array, orderCount: Number } ) // Defining customerSchema model const Customer = mongoose.model( 'Customer' , customerSchema); // Passing empty array in val Customer.find().all( "interest" , []).then((res) => { console.log(res) }) |
Step to run the application: Run the index.js file using the below command:
node index.js
Output:

Please Login to comment...