Skip to content

Commit ab7d99c

Browse files
DOCSP-44706 - Type discriminator note (#342)
Co-authored-by: Stephanie <[email protected]>
1 parent 2db3713 commit ab7d99c

File tree

3 files changed

+46
-3
lines changed

3 files changed

+46
-3
lines changed

source/fundamentals/serialization/polymorphic-objects.txt

+13-1
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,19 @@ you must explicitly list each class you're looking for:
156156
.. code-block:: csharp
157157
:copyable: true
158158

159-
var query = coll.Aggregate().Match(a => a is Cat || a is Lion || a is Tiger);
159+
var query = coll.AsQueryable().Where(
160+
item => item.GetType() == typeof(Cat) ||
161+
item.GetType() == typeof(Lion) ||
162+
item.GetType() == typeof(Tiger));
163+
164+
.. note:: OfType<T>() and the is Operator
165+
166+
When checking the type of a scalar discriminator, use the ``Where`` syntax shown in
167+
the preceding code example. If you try to use the ``Aggregate().OfType<T>()`` method,
168+
or if you pass an expression containing the ``is`` operator to the
169+
``Aggregate().Match()`` method, the driver throws an exception.
170+
171+
.. _csharp-discriminator-hierarchical:
160172

161173
HierarchicalDiscriminatorConvention
162174
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

source/upgrade/v3.txt

+32-1
Original file line numberDiff line numberDiff line change
@@ -203,4 +203,35 @@ Version 3.0 Breaking Changes
203203
property instead.
204204

205205
- The driver removes individual cluster events from ``MongoClient.Cluster``. To listen for
206-
cluster events, use `ClusterBuilder.Subscribe() <{+new-api-root+}/MongoDB.Driver/MongoDB.Driver.Core.Configuration.ClusterBuilder.Subscribe.html>`__.
206+
cluster events, call the `ClusterBuilder.Subscribe() <{+new-api-root+}/MongoDB.Driver/MongoDB.Driver.Core.Configuration.ClusterBuilder.Subscribe.html>`__
207+
method.
208+
209+
- If any type in a collection uses a scalar discriminator, the driver throws
210+
an exception if you perform either of the following actions on the collection:
211+
212+
- Call the ``Aggregate().OfType<T>()`` method, as in the following example:
213+
214+
.. code-block:: csharp
215+
216+
collection.Aggregate().OfType<T>()
217+
218+
- Call the ``Aggregate().Match(item => item is T)`` method, as in the following example:
219+
220+
.. code-block:: csharp
221+
222+
collection.Aggregate().Match(item => item is T)
223+
224+
To use either of the preceding methods on a collection, you can apply a hierarchical
225+
discriminator to each class in the collection. See
226+
the :ref:`Polymorphic Objects <csharp-discriminator-hierarchical>`
227+
page to learn how.
228+
229+
Alternatively, you can check each item's type in a different way. For example, you
230+
can call the ``Where()`` method and pass an expression that compares the item's type
231+
to the type you're looking for, as in the following example:
232+
233+
.. code-block:: csharp
234+
235+
collection.AsQueryable().Where(item => item.GetType() == typeof(T));
236+
237+
To learn more about type discriminators, see :ref:`<csharp-polymorphism>`.

source/whats-new.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ What's New
1515
:values: reference
1616

1717
.. meta::
18-
:keywords: update, new feature, deprecation, upgrade, driver v2.19, driver v2.20, driver v2.21, driver v2.22, driver v2.23, driver v2.24, driver v2.25, driver v2.26, driver v2.27, driver v2.28, driver v3.0
18+
:keywords: update, new feature, deprecation, upgrade, driver v2.22, driver v2.23, driver v2.24, driver v2.25, driver v2.26, driver v2.27, driver v2.28, driver v3.0
1919

2020
Learn what's new in:
2121

0 commit comments

Comments
 (0)