|
4 | 4 | Operations with Builders
|
5 | 5 | ========================
|
6 | 6 |
|
7 |
| -.. default-domain:: mongodb |
8 |
| - |
9 | 7 | .. contents:: On this page
|
10 | 8 | :local:
|
11 | 9 | :backlinks: none
|
12 | 10 | :depth: 2
|
13 | 11 | :class: singlecol
|
14 | 12 |
|
| 13 | +.. facet:: |
| 14 | + :name: genre |
| 15 | + :values: reference |
| 16 | + |
| 17 | +.. meta:: |
| 18 | + :keywords: aggregation, query, code example |
| 19 | + |
15 | 20 | Overview
|
16 | 21 | --------
|
17 | 22 |
|
@@ -337,6 +342,57 @@ as a ``BsonDocument`` to the `AppendStage() method
|
337 | 342 | To learn more about the Aggregation Pipeline, see the
|
338 | 343 | :manual:`Aggregation Pipeline </core/aggregation-pipeline/>` server manual page.
|
339 | 344 |
|
| 345 | +.. _csharp-builders-out: |
| 346 | + |
| 347 | +Write Pipeline Results to a Collection |
| 348 | +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 349 | + |
| 350 | +You can write the documents returned from an aggregation pipeline to a |
| 351 | +collection by creating an ``$out`` stage at the end of your aggregation |
| 352 | +pipeline. To create an ``$out`` stage, call the ``Out()`` method on a |
| 353 | +``PipelineStageDefinitionBuilder``. The ``Out()`` method requires the name of |
| 354 | +the collection you want to write the documents to. |
| 355 | + |
| 356 | +The following example builds an aggregation pipeline that matches all documents |
| 357 | +with a ``season`` field value of ``"Spring"`` and outputs them to |
| 358 | +a ``springFlowers`` collection: |
| 359 | + |
| 360 | +.. code-block:: csharp |
| 361 | + |
| 362 | + var outputCollection = database.GetCollection("springFlowers"); |
| 363 | + var matchFilter = Builders<Flower>.Filter.AnyEq(f => f.Season, "spring"); |
| 364 | + |
| 365 | + // Creates an aggregation pipeline and outputs resulting documents to a new collection. |
| 366 | + var pipeline = new EmptyPipelineDefinition<Flower>() |
| 367 | + .Match(matchFilter) |
| 368 | + .Out(outputCollection); |
| 369 | + |
| 370 | +You can write the results of an aggregation pipeline to a time series collection |
| 371 | +by specifying a ``TimeSeriesOption`` object and passing it as the second |
| 372 | +parameter to the ``Out()`` method. |
| 373 | + |
| 374 | +Imagine that the documents in the ``plants.flowers`` collection contain a ``datePlanted`` field that |
| 375 | +holds BSON date values. You can store the documents in this collection in a time |
| 376 | +series collection by using the ``datePlanted`` field as the time field. |
| 377 | + |
| 378 | +The following example creates a ``TimeSeriesOptions`` object and specifies |
| 379 | +``datePlanted`` as the ``timeField``. It then builds an aggregation pipeline that matches all documents |
| 380 | +with a ``season`` field value of ``"Spring"`` and outputs them to a |
| 381 | +time series collection called ``springFlowerTimes``. |
| 382 | + |
| 383 | +.. code-block:: csharp |
| 384 | + |
| 385 | + var timeSeriesOptions = new TimeSeriesOptions("datePlanted"); |
| 386 | + var collectionName = "springFlowerTimes" |
| 387 | + var matchFilter = Builders<Flower>.Filter.AnyEq(f => f.Season, "spring"); |
| 388 | + |
| 389 | + // Creates an aggregation pipeline and outputs resulting documents to a time series collection. |
| 390 | + var pipeline = new EmptyPipelineDefinition<Flower>() |
| 391 | + .Match(matchFilter) |
| 392 | + .Out(collectionName, timeSeriesOptions); |
| 393 | + |
| 394 | +To learn more about time series collections, see :ref:`csharp-time-series`. |
| 395 | + |
340 | 396 | Build an Atlas Search Query
|
341 | 397 | ---------------------------
|
342 | 398 |
|
|
0 commit comments