- Reference >
- Operators >
- Aggregation Pipeline Operators >
- Pipeline Aggregation Stages >
- $count (aggregation)
$count (aggregation)¶
On this page
Definition¶
-
$count¶ Returns a document that contains a count of the number of documents input to the stage.
$counthas the following prototype form:<string>is the name of the output field which has the count as its value.<string>must be a non-empty string, must not start with$and must not contain the.character.
Example¶
A collection named scores has the following documents:
The following aggregation operation has two stages:
- The
$matchstage excludes documents that have ascorevalue of less than or equal to80to pass along the documents withscoregreater than80to the next stage. - The
$countstage returns a count of the remaining documents in the aggregation pipeline and assigns the value to a field calledpassing_scores.
The operation returns the following results: