- Reference >
- Operators >
- Aggregation Pipeline Stages >
- $documents (aggregation)
$documents (aggregation)¶
On this page
Syntax¶
The $documents stage has the following form:
Behavior¶
$documents accepts any valid expression that resolves to an
array of objects. This includes:
- system variables, such as
$$NOWor$$SEARCH_META $letexpressions- variables in scope from
$lookupexpressions
Expressions that do not resolve to a current document, like
$myField or $$ROOT, will result in an error.
Examples¶
Test a Pipeline Stage¶
Create testing and debugging data for a pipeline stage without creating test collections.
The aggregation expression does not
specify a collection. It uses the input data in the highlighted
$documents stage as input to the $bucketAuto
stage.
Use $documents with $lookup¶
Correlate documents in a collection with other data using
$documents to modify $lookup output.
Create the locations collection.
Use $documents as a data source to transform the documents.
The output correlates the data in the locations collection with the
values in the $documents pipeline stage.
- The
zipfield corresponds to thezip_idfield - The
asparameter creates a new output field
For details on subqueries using this $lookup syntax, see
Correlated Subqueries Using Concise Syntax.