Skip to content

Commit 04b2d39

Browse files
mungitoperritojeff-allen-mongo
authored andcommitted
DOCS-14841 expose documents as stage
1 parent fd90215 commit 04b2d39

File tree

3 files changed

+162
-0
lines changed

3 files changed

+162
-0
lines changed

source/reference/operator/aggregation-pipeline.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -325,6 +325,7 @@ Alphabetical Listing of Stages
325325
/reference/operator/aggregation/collStats
326326
/reference/operator/aggregation/count
327327
/reference/operator/aggregation/currentOp
328+
/reference/operator/aggregation/documents
328329
/reference/operator/aggregation/facet
329330
/reference/operator/aggregation/geoNear
330331
/reference/operator/aggregation/graphLookup
Lines changed: 147 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,147 @@
1+
========================
2+
$documents (aggregation)
3+
========================
4+
5+
.. default-domain:: mongodb
6+
7+
.. contents:: On this page
8+
:local:
9+
:backlinks: none
10+
:depth: 1
11+
:class: singlecol
12+
13+
Definition
14+
----------
15+
16+
.. pipeline:: $documents
17+
18+
.. versionchanged:: 5.1
19+
20+
Returns literal documents from input values.
21+
22+
Syntax
23+
------
24+
25+
The :pipeline:`$documents` stage has the following form:
26+
27+
.. code-block::
28+
29+
{ $documents: <expression> }
30+
31+
Behavior
32+
--------
33+
34+
:pipeline:`$documents` accepts any valid expression that resolves to an
35+
array of objects. This includes:
36+
37+
- system variables, such as :variable:`$$NOW <NOW>` or
38+
``$$SEARCH_META``
39+
- :expression:`$let` expressions
40+
- variables in scope from :pipeline:`$lookup` expressions
41+
42+
Expressions that do not resolve to a current document, like
43+
``$myField`` or :variable:`$$ROOT <ROOT>`, will result in an error.
44+
45+
Examples
46+
--------
47+
48+
Test a Pipeline Stage
49+
~~~~~~~~~~~~~~~~~~~~~
50+
51+
Create testing and debugging data for a pipeline stage without creating
52+
test collections.
53+
54+
.. code-block:: javascript
55+
:emphasize-lines: 3
56+
57+
db.aggregate(
58+
[
59+
{ $documents: [ { x: 10 }, { x: 2 }, { x: 5 } ] },
60+
{ $bucketAuto: { groupBy: "$x", buckets: 4 } }
61+
]
62+
)
63+
64+
The :ref:`aggregation expression <aggregation-expressions>` does not
65+
specify a collection. It uses the input data in the highlighted
66+
:pipeline:`$documents` stage as input to the :pipeline:`$bucketAuto`
67+
stage.
68+
69+
.. code-block:: javascript
70+
:copyable: false
71+
72+
[
73+
{ _id: { min: 2, max: 5 }, count: 1 },
74+
{ _id: { min: 5, max: 10 }, count: 1 },
75+
{ _id: { min: 10, max: 10 }, count: 1 }
76+
]
77+
78+
Use :pipeline:`$documents` with :pipeline:`$lookup`
79+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
80+
81+
Correlate documents in a collection with other data using
82+
:pipeline:`$documents` to modify :pipeline:`$lookup` output.
83+
84+
Create the ``locations`` collection.
85+
86+
.. code-block:: javascript
87+
88+
db.locations.insertMany(
89+
[
90+
{ zip: 94301, name: "Palo Alto" },
91+
{ zip: 10019, name: "New York" }
92+
]
93+
)
94+
95+
Use :pipeline:`$documents` as a data source to transform the documents.
96+
97+
.. code-block:: javascript
98+
99+
db.locations.aggregate(
100+
[
101+
{ $match: {} },
102+
{ $lookup:
103+
{
104+
localField: "zip",
105+
foreignField: "zip_id",
106+
as: "city_state",
107+
pipeline:
108+
[
109+
{ $documents:
110+
[
111+
{ zip_id: 94301, name: "Palo Alto, CA" },
112+
{ zip_id: 10019, name: "New York, NY" }
113+
]
114+
}
115+
]
116+
}
117+
}
118+
]
119+
)
120+
121+
The output correlates the data in the ``locations`` collection with the
122+
values in the :pipeline:`$documents` pipeline stage.
123+
124+
.. code-block:: javascript
125+
:copyable: false
126+
127+
[
128+
{
129+
_id: ObjectId("618949d60f7bfd5f5689490d"),
130+
zip: 94301,
131+
name: 'Palo Alto',
132+
city_state: [ { zip_id: 94301, name: 'Palo Alto, CA' } ]
133+
},
134+
{
135+
_id: ObjectId("618949d60f7bfd5f5689490e"),
136+
zip: 10019,
137+
name: 'New York',
138+
city_state: [ { zip_id: 10019, name: 'New York, NY' } ]
139+
}
140+
]
141+
142+
- The ``zip`` field corresponds to the ``zip_id`` field
143+
- The ``as`` parameter creates a new output field
144+
145+
For details on subqueries using this :pipeline:`$lookup` syntax, see
146+
:ref:`lookup-syntax-concise-correlated-subquery`.
147+

source/release-notes/5.1.txt

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,20 @@ $lookup and $graphLookup with sharded collections
3131

3232
.. include:: /includes/5.1-fact-sharded-lookup-graphlookup.rst
3333

34+
New Aggregation Stages
35+
~~~~~~~~~~~~~~~~~~~~~~
36+
37+
MongoDB 5.1 introduces the following aggregation stages:
38+
39+
.. list-table::
40+
:header-rows: 1
41+
:widths: 20 80
42+
43+
* - Stage
44+
- Description
45+
* - :pipeline:`$documents`
46+
- Returns literal documents from input expressions.
47+
3448
New Aggregation Operators
3549
~~~~~~~~~~~~~~~~~~~~~~~~~
3650

0 commit comments

Comments
 (0)