diff --git a/source/fundamentals/builders.txt b/source/fundamentals/builders.txt index 0f2cdcc5..23f477f0 100644 --- a/source/fundamentals/builders.txt +++ b/source/fundamentals/builders.txt @@ -364,6 +364,42 @@ as a ``BsonDocument`` to the `AppendStage() method To learn more about the Aggregation Pipeline, see the :manual:`Aggregation Pipeline ` server manual page. +.. _csharp-builders-set: + +Add Fields to Documents +~~~~~~~~~~~~~~~~~~~~~~~ + +You can add new fields to documents by creating a ``$set`` stage in +your aggregation pipeline. To create a ``$set`` stage, call the ``Set()`` +method on a ``PipelineStageDefinitionBuilder`` object. + +.. tip:: + + To learn more about the $set stage, see :manual:`$set ` + in the {+mdb-server+} manual. + +This example builds an aggregation pipeline that performs the following +actions: + +- Matches all documents that have a ``Name`` field value of ``"Daffodil"`` +- Adds a ``Color`` field to matching documents and sets its value to + ``"Yellow"`` + +.. code-block:: csharp + + var matchFilter = Builders.Filter.Eq(f => f.Name, "Daffodil"); + var fields = Builders.SetFields.Set("Color", "Yellow"); + + var pipeline = new EmptyPipelineDefinition() + .Match(matchFilter) + .Set(fields); + +The preceding example creates the following pipeline: + +.. code-block:: json + + [{ "$match" : { "Name" : "Daffodil" } }, { "$set" : { "Color" : "Yellow"} }] + .. _csharp-builders-out: Write Pipeline Results to a Collection diff --git a/source/includes/fundamentals/code-examples/builders.cs b/source/includes/fundamentals/code-examples/builders.cs index f4942aa1..aa14ea26 100644 --- a/source/includes/fundamentals/code-examples/builders.cs +++ b/source/includes/fundamentals/code-examples/builders.cs @@ -61,5 +61,6 @@ public class Flower public List Season { get; set; } public double Stock { get; set; } public string Description { get; set; } + public string Color { get; set; } } // end-model \ No newline at end of file