Using single-purpose aggregation

Single-purpose aggregation operators are available so that you can operate on a collection or a cursor. The following table summarizes the operators which can operate on a collection:

db.collection.count() Wraps the $count aggregation operator to produce the number of documents in a collection.
db.collection.distinct()

Wrapper for the distinct command (https://docs.mongodb.com/manual/reference/command/distinct/#distinct). Produces distinct values for document fields across a collection.

 

The following table summarizes single-purpose aggregation operations which can be performed on a cursor (such as the iteration returned after executing db.collection.find()):

cursor.count() Equivalent to db.collection.count() ...

Get MongoDB 4 Quick Start Guide 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.