@@ -89,17 +89,21 @@ in :pipeline:`$setWindowFields`.
89
89
Comparison of :pipeline:`$fill` and :group:`$linearFill`
90
90
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
91
91
92
- The :pipeline:`$fill` stage with ``{ method: "linear" }`` and the
93
- :group:`$linearFill` operator both fill missing values using
94
- :wikipedia:`linear interpolation <Linear_interpolation>`.
92
+ To fill missing field values using
93
+ :wikipedia:`linear interpolation <Linear_interpolation>`, you can use:
95
94
96
- - When you use the :pipeline:`$fill` stage, the field you fill must be
97
- the same as the field you fill from.
95
+ - The :pipeline:`$fill` stage with ``{ method: "linear" }``.
98
96
99
- - When you use the :group:`$linearFill` operator inside of a
100
- :pipeline:`$setWindowFields` stage, you can set values for a
101
- different field than the field used as the source data. For an
102
- example, see :ref:`linearFill-example-multiple-methods`.
97
+ When you use the :pipeline:`$fill` stage, the field you specify in the
98
+ output is the same field used as the source data. See
99
+ :ref:`fill-example-linear`.
100
+
101
+ - The :group:`$linearFill` operator inside of a
102
+ :pipeline:`$setWindowFields` stage.
103
+
104
+ When you use the :group:`$linearFill` operator, you can set values
105
+ for a different field than the field used as the source data. See
106
+ :ref:`linearFill-example-multiple-methods`.
103
107
104
108
.. _linearFill-example:
105
109
@@ -217,100 +221,7 @@ Example output:
217
221
Use Multiple Fill Methods in a Single Stage
218
222
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
219
223
220
- When you use the :pipeline:`$setWindowFields` stage to fill missing
221
- values, you can set values for a different field than the field you
222
- fill from. As a result, you can use multiple fill methods in a single
223
- :pipeline:`$setWindowFields` stage and output the results in distinct
224
- fields.
225
-
226
- The following pipeline populates missing ``price`` fields using
227
- |linear-interpolation| and the last-observation-carried-forward method:
228
-
229
- .. code-block:: javascript
230
-
231
- db.stock.aggregate( [
232
- {
233
- $setWindowFields:
234
- {
235
- sortBy: { time: 1 },
236
- output:
237
- {
238
- linearFillPrice: { $linearFill: "$price" },
239
- locfPrice: { $locf: "$price" }
240
- }
241
- }
242
- }
243
- ] )
244
-
245
- In the example:
246
-
247
- - ``sortBy: { time: 1 }`` sorts the documents by the ``time`` field in
248
- ascending order, from earliest to latest.
249
-
250
- - :ref:`output <setWindowFields-output>` specifies:
251
-
252
- - ``linearFillPrice`` as a target field to be filled.
253
-
254
- - ``{ $linearFill: "$price" }`` is the value for the
255
- ``linearFillPrice`` field. :group:`$linearFill` fills missing
256
- ``price`` values using |linear-interpolation| based on the
257
- surrounding ``price`` values in the sequence.
258
-
259
- - ``locfPrice`` as a target field to be filled.
260
-
261
- - ``{ $locf: "$price" }`` is the value for the ``locfPrice`` field.
262
- ``locf`` stands for last observation carried forward.
263
- :group:`$locf` fills missing ``price`` values with the value from
264
- the previous document in the sequence.
265
-
266
- Example output:
267
-
268
- .. code-block:: javascript
269
- :copyable: false
270
- :emphasize-lines: 12,13,25,26,31,32
271
-
272
- [
273
- {
274
- _id: ObjectId("620ad555394d47411658b5ef"),
275
- time: ISODate("2021-03-08T09:00:00.000Z"),
276
- price: 500,
277
- linearFillPrice: 500,
278
- locfPrice: 500
279
- },
280
- {
281
- _id: ObjectId("620ad555394d47411658b5f0"),
282
- time: ISODate("2021-03-08T10:00:00.000Z"),
283
- linearFillPrice: 507.5,
284
- locfPrice: 500
285
- },
286
- {
287
- _id: ObjectId("620ad555394d47411658b5f1"),
288
- time: ISODate("2021-03-08T11:00:00.000Z"),
289
- price: 515,
290
- linearFillPrice: 515,
291
- locfPrice: 515
292
- },
293
- {
294
- _id: ObjectId("620ad555394d47411658b5f2"),
295
- time: ISODate("2021-03-08T12:00:00.000Z"),
296
- linearFillPrice: 505,
297
- locfPrice: 515
298
- },
299
- {
300
- _id: ObjectId("620ad555394d47411658b5f3"),
301
- time: ISODate("2021-03-08T13:00:00.000Z"),
302
- linearFillPrice: 495,
303
- locfPrice: 515
304
- },
305
- {
306
- _id: ObjectId("620ad555394d47411658b5f4"),
307
- time: ISODate("2021-03-08T14:00:00.000Z"),
308
- price: 485,
309
- linearFillPrice: 485,
310
- locfPrice: 485
311
- }
312
- ]
313
-
224
+ .. include:: /includes/example-multiple-fill-methods.rst
314
225
315
226
Restrictions
316
227
------------
0 commit comments