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

Closed
wants to merge 3 commits into from
Closed
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.

Copy link
Collaborator

Choose a reason for hiding this comment

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

The current implementation of Set assumes that you are using Set to change the value of existing fields.

I will be creating a JIRA ticket to cover the scenario where you want to use Set to add new fields. This will require adding a TNewResult type parameter to represent the new result with the new additional fields.

But we should wait until that gets implemented to update the documentation.

So for now let's document setting existing fields.

Copy link
Collaborator

Choose a reason for hiding this comment

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

p.s. I wrote the above comment a long time ago but apparently didn't submit it because it still showed as "pending".

Copy link
Collaborator

Choose a reason for hiding this comment

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

So I think that means we should not merge this PR at this time.


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