Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get a count of total documents with MongoDB when using limit

I am interested in optimizing a "pagination" solution I'm working on with MongoDB. My problem is straight forward. I usually limit the number of documents returned using the limit() functionality. This forces me to issue a redundant query without the limit() function in order for me to also capture the total number of documents in the query so I can pass to that to the client letting them know they'll have to issue an additional request(s) to retrieve the rest of the documents.

Is there a way to condense this into 1 query? Get the total number of documents but at the same time only retrieve a subset using limit()? Is there a different way to think about this problem than I am approaching it?

like image 865
randombits Avatar asked Feb 15 '14 20:02

randombits


People also ask

How do I count the number of documents in MongoDB?

Description. n = count( conn , collection ) returns the total number of documents in a collection by using the MongoDB connection. n = count( conn , collection ,'Query', mongoquery ) returns the total number of documents in an executed MongoDB query on a collection.

Where is total count in MongoDB?

count() method is used to return the count of documents that would match a find() query. The db. collection. count() method does not perform the find() operation but instead counts and returns the number of results that match a query.

How does MongoDB define maximum number of documents in collection?

Unless you create a capped collection, there is no limit to the number of documents in a collection. There is a 16MB limit to the size of a document (use gridfs in this situation) and limits in the storage engine for the size of the database and data.

How do I count documents in a collection?

var value = db. collection. count(); and then print(value) or simply value , would give you the count of documents in the collection named collection .


2 Answers

Mongodb 3.4 has introduced $facet aggregation

which processes multiple aggregation pipelines within a single stage on the same set of input documents.

Using $facet and $group you can find documents with $limit and can get total count.

You can use below aggregation in mongodb 3.4

db.collection.aggregate([   { "$facet": {     "totalData": [       { "$match": { }},       { "$skip": 10 },       { "$limit": 10 }     ],     "totalCount": [       { "$group": {         "_id": null,         "count": { "$sum": 1 }       }}     ]   }} ]) 

Even you can use $count aggregation which has been introduced in mongodb 3.6.

You can use below aggregation in mongodb 3.6

db.collection.aggregate([   { "$facet": {     "totalData": [       { "$match": { }},       { "$skip": 10 },       { "$limit": 10 }     ],     "totalCount": [       { "$count": "count" }     ]   }} ]) 
like image 153
Ashh Avatar answered Sep 20 '22 09:09

Ashh


No, there is no other way. Two queries - one for count - one with limit. Or you have to use a different database. Apache Solr for instance works like you want. Every query there is limited and returns totalCount.

like image 44
heinob Avatar answered Sep 22 '22 09:09

heinob