Open In App

Mongoose Query.prototype.lean() API

Last Updated : 27 Mar, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

The Mongoose Query API.prototype.lean() method of the Mongoose API is used on the Query objects. It allows us to know whether the returned document is a Javascript object or a mongoose document object. Using this method we can configure the options for the query object. If the lean option is enabled for the document returned from the query object that means they are javascript objects, not mongoose objects. We can not use save, getter/setter methods on them. Let us understand the lean() method using an example.

Syntax:

query.lean( <boolean> );

Parameters: This method accepts a single parameter as described below.

  • bool: It is used to specify a boolean value. Either true or false. If set to true i.e returned value from the query object is a Javascript object else it is the mongoose document object.

Return Value: This method returns the Query object.

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 is present in the MongoDB.

 

Example 1: The below example illustrates the basic functionality of the Mongoose Connection lean() method. In this example, we are comparing the resulting object with the mongoose Document class reference.

Filename: app.js

Javascript




// Require mongoose module
const mongoose = require("mongoose");
 
// Set Up the Database connection
 
let connectionObject = mongoose.createConnection(URI, {
    useNewUrlParser: true,
    useUnifiedTopology: true,
});
 
let studentSchema = new mongoose.Schema({
    name: { type: String, required: true },
    age: Number,
    rollNumber: { type: Number, required: true }
});
 
let StudentModel = connectionObject.model('Student',
    studentSchema);
 
(async () => {
    let doc = await StudentModel.find({ name: "Student1" });
    console.log(doc[0] instanceof mongoose.Document);
});


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 lean() method. In this example, we can easily differentiate that the result set is the instance of the JavaScript object and is not an instance of the mongoose Document class.

Filename: app.js

Javascript




// Require mongoose module
const mongoose = require("mongoose");
 
// Set Up the Database connection
 
let connectionObject = mongoose.createConnection(URI, {
    useNewUrlParser: true,
    useUnifiedTopology: true,
});
 
let studentSchema = new mongoose.Schema({
    name: { type: String, required: true },
    age: Number,
    rollNumber: { type: Number, required: true }
});
 
let StudentModel = connectionObject.model('Student',
    studentSchema);
 
StudentModel.find({ name: "Student1" }).lean(true).then(res => {
    console.log(res[0] instanceof mongoose.Document);
    console.log(res[0] instanceof Object)
});


Step to run the program: To run the application execute the below command from the root directory of the project:

node app.js

Output:

false
true

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



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads