Pankaj Shinde
Pankaj Shinde

Reputation: 3689

Why mongo db (version 3.0.6) returns wrong number of records when we use count with limit option?

As per mongo db doc says we can use count with limit.

Limit option is used to specify the maximum number of documents the cursor will return. But if we use limit with count it returns total count and not correct count.

Why?

Suppose we have 50 records in collection then only count option will return 50, and if we apply limit(10) option then it should return 10 and not 50. But count with limit returns 50.

Upvotes: 1

Views: 350

Answers (1)

Vishnu
Vishnu

Reputation: 12293

db.collection.find(<query>).count();

You will get count of all records found after executing the query. i.e count=50;

db.collection.find(<query>).limit(10).count(true);

You will get the count of limited documents. i.e count=10.

You should set applySkipLimit to true.

http://docs.mongodb.org/manual/reference/method/cursor.count/

Upvotes: 1

Related Questions