1

I'm trying to get the count all the data for the query and as well as get the limited data (for pagination) but both the console output gives me the count. What is that I'm doing wrong. Pls help

const curFind = fbUser.find(find, 'firstName gender age birthday facebookId profileURL email imageUrl preferences blocked flames likes rejects location reported')

  curFind.count(function(e, count) {
    console.log(count);
    curFind.skip(0).limit(10).hint( { $natural : 1 } ).exec(function(err, data) {
      console.log(data);
    });
  });
1
  • Could you elaborate bit more for better understanding. Dec 27 '17 at 4:52
1

As per MongoDB BLOG Here In certain situations, count can be avoided for better performance Counting things can be hard on a database. The obvious things to count, number of items in a collection or index, are easy to keep track of, but as soon as you start trying to count records based on a query, things get slower and slower. This is because the database has to do more work scanning practically every item in the database to see if it matches.

MongoDB does have an optimisation for a fast count where all the queried fields are indexed and the query is based around equivalence, but only in that circumstance.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.