Returning total for paginated searching

Hi,
How can we return the of total number of documents returned in a mongodb atlas search when it is being paginated.

For example, If we have 11 matching results when doing the search, but pagination should show 10 results per page. when using the limit argument it will only return 10 documents, but we need to return the total number of results matched (11) during the search as well.

Hi,

Do you mean the db.find() operation? If so you can append a .count() to get the total: db.mycol.find({...}).count();
If you mean the $search in the Mongo Atlas views then you probably can add a $count stage to the aggregation pipeline when executed from your application.

1 Like

2 posts were split to a new topic: For a big collection of documents, it is faster to execute 2 queries or use aggregations?