Open In App

Mongoose Query prototype.size() API

Last Updated : 23 Oct, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

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 size( ) method of mongoose Query API is used to find the documents that have a field that contains an array of specified size.

Syntax:

Modal.where([path]).size([val]).exec()

 

Parameters:

  • path: path is a string that represents the field name, this is a must-have parameter.
  • val: val is the number of elements in the array.

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 of customers that contains their names and gaming interests. In this example, we want to find customers who are interested in 5 games.

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 the number of customers who are interested in 5 games
Customer.where("interest").size(5).exec().then((res) => {
    console.log(res)
});


Steps to run the application:

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 are passing the size of the array to any number that any field does not contain that number of array.

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 the number of customers who are interested in 500 games
Customer.where("interest").size(500).exec().then((res) => {
    console.log(res)
});


Step to run the application: Run the index.js file using the below command:

node index.js

Output:

 



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads