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
Show file tree
Hide file tree
Changes from all commits
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
36 changes: 36 additions & 0 deletions source/fundamentals/builders.txt
Original file line number Diff line number Diff line change
Expand Up @@ -364,6 +364,42 @@
To learn more about the Aggregation Pipeline, see the
:manual:`Aggregation Pipeline </core/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 </reference/operator/aggregation/set/>`

Check failure on line 378 in source/fundamentals/builders.txt

View workflow job for this annotation

GitHub Actions / TDBX Vale rules

[vale] reported by reviewdog 🐶 [MongoDB.Wordiness] Consider using 'consider' instead of 'take into account'. Raw Output: {"message": "[MongoDB.Wordiness] Consider using 'consider' instead of 'take into account'.", "location": {"path": "source/fundamentals/builders.txt", "range": {"start": {"line": 378, "column": 8}}}, "severity": "ERROR"}
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
1 change: 1 addition & 0 deletions source/includes/fundamentals/code-examples/builders.cs
Original file line number Diff line number Diff line change
Expand Up @@ -61,5 +61,6 @@ public class Flower
public List<string> Season { get; set; }
public double Stock { get; set; }
public string Description { get; set; }
public string Color { get; set; }
}
// end-model
Loading