Open In App

How does maxScan() work in Mongoose ?

Last Updated : 17 Mar, 2021
Improve
Improve
Like Article
Like
Save
Share
Report

The maxScan() function is used to specify the maxScan option. This option tell the MongoDB server to set the maximum limit of scanning documents when it performs scan operation.
 

Syntax:  

maxScan()

Parameters: This function has one val parameter of number type.
Return Value: This function returns Query Object.
 

Installing mongoose module:

npm install mongoose

After installing the mongoose module, you can check your mongoose version in command prompt using the command. 

npm mongoose --version

After that, you can just create a folder and add a file for example, index.js as shown below.

Database: The sample database used here is shown below: 
 

Project Structure: The project structure will look like this: 
 

Example 1:

index.js




const mongoose = require('mongoose');
  
// Database connection
    useNewUrlParser: true,
    useCreateIndex: true,
    useUnifiedTopology: true
});
  
// User model
const User = mongoose.model('User', { 
    name: { type: String },
    age: { type: Number }
});
  
const query = User.find();
query.where().maxScan(100)
  
console.log("Max Scan set to", query.options)


Run index.js file using below command: 

node index.js

Output: 

Max Scan set to { maxScan: 100 }

Example 2:

index.js




const express = require('express');
const mongoose = require('mongoose');
const app = express()
  
// Database connection
    useNewUrlParser: true,
    useCreateIndex: true,
    useUnifiedTopology: true
});
  
// User model
const User = mongoose.model('User', { 
    name: { type: String },
    age: { type: Number }
});
  
const query = User.find();
query.where().maxScan(55)
  
console.log("Max Scan value:", query.options)
  
app.listen(3000, function(error ) {
    if(error) console.log(error)
    console.log("Server listening on PORT 3000")
});


Run index.js file using below command: 

node index.js

Output: 

Max Scan value: { maxScan: 55 }
Server listening on PORT 3000

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



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads