Skip to content

Set() stage builder #512

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 37 additions & 0 deletions source/fundamentals/builders.txt
Original file line number Diff line number Diff line change
Expand Up @@ -364,6 +364,43 @@ as a ``BsonDocument`` to the `AppendStage() method
To learn more about the Aggregation Pipeline, see the
:manual:`Aggregation Pipeline </core/aggregation-pipeline/>` server manual page.

.. _csharp-builders-set:

Add Fields to Collection Documents
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

You can add new fields to existing documents in a collection
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This might be a bit misleading because the documents in the collection are NOT modified.

The new fields are only added to the documents going through the pipeline, the original documents that are input to the pipeline are unchanged.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Okay, I removed mentions of "collection" and just kept it as "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 </reference/operator/aggregation/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<Flower>.Filter.Eq(f => f.Name, "Daffodil");
var fields = Builders<Flower>.SetFields.Set("Color", "Yellow");

var pipeline = new EmptyPipelineDefinition<Flower>()
.Match(matchFilter)
.Set(fields);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Have you tried executing this?

Most likely it will throw an exception when reading the results from the server because the C# declaration for Flower does not have a Color property.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah yep, I added Color to the Flower class while testing and just added it to the page. But does that defeat the purpose of Set()?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point. It looks like the API for Set is defective. Let me discuss with the team.


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
Expand Down
Loading