|
| 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 | + |
0 commit comments