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 ...

Get Node.js, MongoDB, and AngularJS Web Development now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.