2

I am trying to use lean in my mongoDB query but the problem I am facing is that I don't use .exec() method. I use callback implementation like below

model.find({user_id: mobile_no }, {'_id':0, 'type':1}, {sort: {dateTime: -1}, skip: page*page_size, limit: page_size + 1}, function(err, docs) {
     if (err) {
                
     } else {

     }
});

but in most documentation,everyone uses lean with .exec() like below

.lean().exec()

Can anyone please tell me how can I use lean using my callback implementation or I would have to use .exec() implementation in order to use it.

2 Answers 2

1

You can use both .exec() and callbacks:

model.find(...).lean().exec(function(err, docs) {
  ...
});

See also the documentation, where one of the examples does the same.

Sign up to request clarification or add additional context in comments.

6 Comments

yes but as i am not using exec() can i use lean() without using exec() because otherwise i will have to change code everywhere just because of exec().
I don't understand: you already have to change your code everywhere because you want to use .lean(), so what's the issue with adding an additional .exec()? Like my answer states: you can still use callbacks using .exec(), you don't have to use promises if you don't want to.
ok thanks i just want to use my syntax, there is no extra problem thanks anyways.
actually time taken by query is still little bit same, there is no significant difference coming ?? Can you tell me why this is hapening.
My guess would be that your query just is slow, which lean won't solve (it only skips the step where Mongoose converts the MongoDB results into Mongoose documents).
|
0
model.find({user_id: mobile_no }, {'_id':0, 'type':1}, {sort: {dateTime: -1}, skip: page*page_size, limit: page_size + 1}, function(err, docs) {
     if (err) {

     } else {

     }
}).lean();

You have to write .lean() at the end of the query :3

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.