Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In Mongodb, how do I get the count of the total results returned, without the limit?

Let's say i put a limit and skip on the MongoDB query...I want to know the total results if there was not a limit on there.

Of course, I could do this the shitty way...which is to query twice.

like image 467
TIMEX Avatar asked Jun 16 '11 06:06

TIMEX


People also ask

How do I count the number of records in MongoDB?

MongoDB count() Method – db. Collection. count() The count() method counts the number of documents that match the selection criteria.

How does skip and limit work in MongoDB?

The limit() function in MongoDB is used to specify the maximum number of results to be returned. Only one parameter is required for this function.to return the number of the desired result. Sometimes it is required to return a certain number of results after a certain number of documents. The skip() can do this job.

Can we use count with aggregate function in MongoDB?

MongoDB $count AggregationThe MongoDB $count operator allows us to pass a document to the next phase of the aggregation pipeline that contains a count of the documents. There a couple of important things to note about this syntax: First, we invoke the $count operator and then specify the string.


1 Answers

In MongoDB the default behavior of count() is to ignore skip and limit and count the number of results in the entire original query. So running count will give you exactly what you want.

Passing a Boolean true to count or calling size instead would give you a count WITH skip or limit.

like image 163
Brendan W. McAdams Avatar answered Nov 15 '22 21:11

Brendan W. McAdams