Skip to content

Commit 0236653

Browse files
authored
DOCSP-49279: $convert stage LINQ method (mongodb#604)
* DOCSP-49279: * typo fix * JS PR fixes 1 * FP tech review 1 * small fix * parentheses fix
1 parent e66bd23 commit 0236653

File tree

2 files changed

+69
-7
lines changed

2 files changed

+69
-7
lines changed

source/fundamentals/linq.txt

Lines changed: 57 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -135,17 +135,17 @@ You can print the results of the preceding example as follows:
135135

136136
var results = query.ToCursor();
137137

138-
139138
Supported Aggregation Stages
140139
----------------------------
141140

142-
You can use LINQ to create an :ref:`aggregation pipeline <aggregation-pipeline-intro>`.
141+
You can use LINQ to create an :manual:`aggregation pipeline </core/aggregation-pipeline/>`.
143142
The {+driver-short+} automatically translates each LINQ statement into the corresponding
144143
aggregation pipeline stages. In this section you can learn which
145144
aggregation pipeline stages are supported.
146145

147-
To learn more about the aggregation pipeline stages, see the
148-
:ref:`aggregation-pipeline-operator-reference` page in the server manual.
146+
To learn more about the aggregation pipeline stages, see
147+
:manual:`Aggregation Stages </reference/operator/aggregation-pipeline/>`
148+
in the {+mdb-server+} manual.
149149

150150
$project
151151
~~~~~~~~
@@ -519,7 +519,7 @@ The ``$group`` aggregation stage separates documents into groups according to
519519
the criteria you specify.
520520

521521
Select the :guilabel:`Method Syntax` or :guilabel:`Query Syntax` tab to see how
522-
to generate an ``$group`` stage using LINQ:
522+
to generate a ``$group`` stage using LINQ:
523523

524524
.. tabs::
525525

@@ -570,7 +570,7 @@ The ``$sort`` aggregation stage returns the results of your query in the order
570570
that you specify.
571571

572572
Select the :guilabel:`Method Syntax` or :guilabel:`Query Syntax` tab to see how
573-
to generate an ``$sort`` stage using LINQ:
573+
to generate a ``$sort`` stage using LINQ:
574574

575575
.. tabs::
576576

@@ -793,6 +793,16 @@ in the Atlas manual. For more examples about running Atlas Vector Search queries
793793
{+driver-short+}, see :atlas:`Run Vector Search Queries </atlas-vector-search/vector-search-stage/>`
794794
in the Atlas manual and select :guilabel:`C#` from the language dropdown.
795795

796+
Aggregation Operators
797+
---------------------
798+
799+
You can use :manual:`aggregation pipeline operators
800+
</reference/operator/aggregation/>` in your aggregation stages to modify
801+
documents and perform calculations.
802+
803+
The following sections describe aggregation operators that you can
804+
implement by using LINQ methods.
805+
796806
Bitwise Operators
797807
~~~~~~~~~~~~~~~~~
798808

@@ -967,6 +977,42 @@ The result contains the following values:
967977
0
968978
1
969979

980+
.. _csharp-linq-convert:
981+
982+
$convert
983+
~~~~~~~~
984+
985+
The ``$convert`` operator converts a value to a specified type. You can
986+
use this operator to perform type conversions in stages such as
987+
``$project``, ``$addFields``, and ``$set``.
988+
989+
In the driver, you can use the ``Mql.Convert()`` method to
990+
convert a value from one type to a different specified type. To learn
991+
more about conversion behavior and permitted conversions, see the
992+
:manual:`$convert reference </reference/operator/aggregation/convert/>`
993+
in the {+mdb-server+} manual.
994+
995+
The ``Convert()`` method takes the following parameters:
996+
997+
- Value to convert.
998+
- ``ConvertOptions`` instance that specifies the type to convert to
999+
and options. Some conversions require you to specify certain options, but
1000+
you can also set options to handle errors or null values.
1001+
1002+
The following code performs the following actions by using LINQ methods:
1003+
1004+
- Converts the ``RestaurantId`` string values to ``int`` values in a
1005+
``Select()`` projection
1006+
- Sets the returned value to ``-1`` if an error occurs during conversion
1007+
- Sets the returned value to ``0`` if the input value is ``null`` or missing
1008+
1009+
.. code-block:: csharp
1010+
1011+
var query = queryableCollection
1012+
.Select(r => Mql.Convert(r.RestaurantId, new ConvertOptions<int> { OnError = -1, OnNull = 0 }));
1013+
1014+
The driver stores the converted values under the original field name in
1015+
the output documents.
9701016

9711017
Unsupported Aggregation Stages
9721018
------------------------------
@@ -984,7 +1030,8 @@ the :ref:`<csharp-builders-out>` section.
9841030
Supported Methods
9851031
-----------------
9861032

987-
The following are some methods supported by the {+driver-long+} implementation of LINQ:
1033+
The following table describes some methods supported by the
1034+
{+driver-long+} implementation of LINQ:
9881035

9891036
.. list-table::
9901037
:header-rows: 1
@@ -1005,6 +1052,9 @@ The following are some methods supported by the {+driver-long+} implementation o
10051052
* - ``LongCount``
10061053
- Returns an ``Int64`` that represents the number of documents that match the specified criteria
10071054

1055+
* - ``Convert``
1056+
- Converts a value from one type to a different specified type
1057+
10081058
* - ``DateFromString``
10091059
- Converts a ``string`` to a ``DateTime`` object
10101060

source/whats-new.txt

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ What's New
2020

2121
Learn what's new in:
2222

23+
* :ref:`Version 3.4 <csharp-version-3.4>`
2324
* :ref:`Version 3.3 <csharp-version-3.3>`
2425
* :ref:`Version 3.2 <csharp-version-3.2>`
2526
* :ref:`Version 3.1 <csharp-version-3.1>`
@@ -41,6 +42,17 @@ In accordance with the `MongoDB Software Lifecycle Schedules
4142
version of {+driver-short+} will raise the minimum {+mdb-server+} version from
4243
4.0 to 4.2. {+driver-short+} will no longer support {+mdb-server+} 4.0.
4344

45+
.. _csharp-version-3.4:
46+
47+
What's New in 3.4
48+
-----------------
49+
50+
The 3.4 driver release includes the following new features:
51+
52+
- Adds the ``Mql.Convert()`` LINQ method to convert between types when
53+
performing aggregations. To learn more, see the
54+
:ref:`csharp-linq-convert` section of the LINQ guide.
55+
4456
.. _csharp-version-3.3:
4557

4658
What's New in 3.3

0 commit comments

Comments
 (0)