DEV Community

Cover image for Get MongoDB data 10x smaller & faster using lean function in NodeJs
Deepak Jaiswal
Deepak Jaiswal

Posted on

9 3

Get MongoDB data 10x smaller & faster using lean function in NodeJs

sometimes, i am finding data in mongoose collection it give mongoose document but when we use lean function in find collection it gives response 10x smaller and faster as compare to simple finding collection. mongoose also say this its gives 10x smaller response.

product.model.js

const mongoose = require("mongoose")

const ProductSchema = new mongoose.Schema({
    name:{
        type:String,
        required:true,
                index:true,
        trim:true
    },
        createdBy:{
                type:mongoose.Types.Schema.ObjectId,
                ref:'user',
                required:true
        },
    color:{
        type:String,
        required:true,
        trim:true
    },
    ram:{
        type:String,
        required:true
    },
    rom:{
        type:String,
        required:true
    },
    price:{
        type:String,
        required:true
    },
    qty:{
        type:String,
        required:true,
        default:1
    },
    displaySize:{
        type:String,
        required:true
    },
    frontCamera:{
        type:String,
        required:true
    },
    rearCamera:{
        type:String,
        required:true
    },
    battery:{
        type:String,
        required:true
    },
    processor:{
        type:String,
        required:true
    },
    imageUrl:{
        type:String,
        required:true
    },
    modelNumber:{
        type:String,
        required:true
    },
    modelName:{
        type:String,
        required:true
    },
    operatingSystem:{
        type:String,
        required:true
    },
    warrenty:{
        type:String,
                default:"6 months"
    },
    addDate:{
        type:Date,
                default:Date.now
    }
})

module.exports = mongoose.model('Product',ProductSchema)
Enter fullscreen mode Exit fullscreen mode

product.controller.js

without lean function

module.exports.listAllProducts =async (req,res,next)=>{
  try{
    let products=await Product.find()
                .populate("createdBy").sort({addDate:-1})
        res.send(products);
  }catch(err){
    next(err)
  }
}
Enter fullscreen mode Exit fullscreen mode

with lean function

module.exports.listAllProducts =async (req,res,next)=>{
  try{
    let products=await Product.find().populate("createdBy")
               .lean().sort({addDate:-1})
        res.send(products);
  }catch(err){
    next(err)
  }
}
Enter fullscreen mode Exit fullscreen mode

when we use lean function we cannot modify document value and save it.

Postgres on Neon - Get the Free Plan

No credit card required. The database you love, on a serverless platform designed to help you build faster.

Get Postgres on Neon

Top comments (6)

Collapse
 
abdurrkhalid333 profile image
Abdur Rehman Khalid

I am looking forward to working with the "lean" function as I have a lot of data and it takes a lot of time as well. So, I definitely have to try this function in my production environment as well.

Collapse
 
deepakjaiswal profile image
Deepak Jaiswal • Edited

lean function reduce the size of document so its affect the response.
check all details
mongoosejs.com/docs/tutorials/lean...
if you have more data you can use compression npm package

Collapse
 
abdurrkhalid333 profile image
Abdur Rehman Khalid

thanks for mentioning this here, I will have a deeper look into that as well.

Collapse
 
pyrsmk profile image
Aurélien Delogu

Just to point out that this is only a Mongoose trick to avoid model objects hydration after a query (just saying, because the title say something else ^^).

Collapse
 
deepakjaiswal profile image
Deepak Jaiswal

You are right. Thank you.

Collapse
 
deepakjaiswal profile image
Deepak Jaiswal

Here createdBy is reference of user model.
Thank you.

Billboard image

Create up to 10 Postgres Databases on Neon's free plan.

If you're starting a new project, Neon has got your databases covered. No credit cards. No trials. No getting in your way.

Try Neon for Free →

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay