Skip to content

DOCSP-44706 - Type discriminator note #342

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

Merged
merged 4 commits into from
Dec 4, 2024
Merged
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
14 changes: 13 additions & 1 deletion source/fundamentals/serialization/polymorphic-objects.txt
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,19 @@ you must explicitly list each class you're looking for:
.. code-block:: csharp
:copyable: true

var query = coll.Aggregate().Match(a => a is Cat || a is Lion || a is Tiger);
var query = coll.AsQueryable().Where(
item => item.GetType() == typeof(Cat) ||
item.GetType() == typeof(Lion) ||
item.GetType() == typeof(Tiger));

.. note:: OfType<T>() and the is Operator

When checking the type of a scalar discriminator, use the ``Where`` syntax shown in
the preceding code example. If you try to use the ``Aggregate().OfType<T>()`` method,
or if you pass an expression containing the ``is`` operator to the
``Aggregate().Match()`` method, the driver throws an exception.

.. _csharp-discriminator-hierarchical:
Copy link
Collaborator

Choose a reason for hiding this comment

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

This limitation will go away when https://jira.mongodb.org/browse/CSHARP-5356 is merged.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

We'll publish this now and can update in 3.2 or whenever the fix is implemented.


HierarchicalDiscriminatorConvention
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Expand Down
33 changes: 32 additions & 1 deletion source/upgrade/v3.txt
Original file line number Diff line number Diff line change
Expand Up @@ -203,4 +203,35 @@ Version 3.0 Breaking Changes
property instead.

- The driver removes individual cluster events from ``MongoClient.Cluster``. To listen for
cluster events, use `ClusterBuilder.Subscribe() <{+new-api-root+}/MongoDB.Driver/MongoDB.Driver.Core.Configuration.ClusterBuilder.Subscribe.html>`__.
cluster events, call the `ClusterBuilder.Subscribe() <{+new-api-root+}/MongoDB.Driver/MongoDB.Driver.Core.Configuration.ClusterBuilder.Subscribe.html>`__
method.

- If any type in a collection uses a scalar discriminator, the driver throws
an exception if you perform either of the following actions on the collection:

- Call the ``Aggregate().OfType<T>()`` method, as in the following example:

.. code-block:: csharp

collection.Aggregate().OfType<T>()

- Call the ``Aggregate().Match(item => item is T)`` method, as in the following example:

.. code-block:: csharp

collection.Aggregate().Match(item => item is T)

To use either of the preceding methods on a collection, you can apply a hierarchical
discriminator to each class in the collection. See
the :ref:`Polymorphic Objects <csharp-discriminator-hierarchical>`
page to learn how.

Alternatively, you can check each item's type in a different way. For example, you
can call the ``Where()`` method and pass an expression that compares the item's type
to the type you're looking for, as in the following example:

.. code-block:: csharp

collection.AsQueryable().Where(item => item.GetType() == typeof(T));

To learn more about type discriminators, see :ref:`<csharp-polymorphism>`.
3 changes: 1 addition & 2 deletions source/whats-new.txt
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ What's New
:values: reference

.. meta::
: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
: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

Learn what's new in:

Expand All @@ -29,7 +29,6 @@ Learn what's new in:
* :ref:`Version 2.24 <version-2.24>`
* :ref:`Version 2.23 <version-2.23>`
* :ref:`Version 2.22 <version-2.22>`
* :ref:`Version 2.21 <version-2.21>`

.. _upcoming-breaking-changes:

Expand Down
Loading