Skip to content

Commit 32c30bd

Browse files
DOCSP-34852 Support $out (#207)
1 parent 5b5ca74 commit 32c30bd

File tree

2 files changed

+61
-2
lines changed

2 files changed

+61
-2
lines changed

source/fundamentals/builders.txt

Lines changed: 58 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,19 @@
44
Operations with Builders
55
========================
66

7-
.. default-domain:: mongodb
8-
97
.. contents:: On this page
108
:local:
119
:backlinks: none
1210
:depth: 2
1311
:class: singlecol
1412

13+
.. facet::
14+
:name: genre
15+
:values: reference
16+
17+
.. meta::
18+
:keywords: aggregation, query, code example
19+
1520
Overview
1621
--------
1722

@@ -337,6 +342,57 @@ as a ``BsonDocument`` to the `AppendStage() method
337342
To learn more about the Aggregation Pipeline, see the
338343
:manual:`Aggregation Pipeline </core/aggregation-pipeline/>` server manual page.
339344

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+
340396
Build an Atlas Search Query
341397
---------------------------
342398

source/fundamentals/linq.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -507,6 +507,9 @@ aggregation stages:
507507
- ``$geoNear``
508508
- ``$out``
509509

510+
To learn how to create an aggregation pipeline with the ``$out`` stage by using Builders, see
511+
the :ref:`<csharp-builders-out>` section.
512+
510513
Supported Methods
511514
-----------------
512515

0 commit comments

Comments
 (0)