Counting Documents
When accessing document sets in MongoDB, you might want to get a count first and then decide whether to retrieve a set of documents. There are several reasons to count specific document sets. Performing a count is much less intensive on the MongoDB side than retrieving documents using find() and other methods, which cause temporary objects such as Cursor objects to be created and maintained by the server.
When performing operations on the resulting set of documents from a find(), you should be aware of how many documents you are going to be dealing with, especially in larger environments. Sometimes all you want is a count. For example, if you need to know how many users are configured in your application, you could just count ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access