Skip to content

Commit 1352bb4

Browse files
authored
DOCSP-39924: Update eligible SDKs with collections in mixed info (#3293)
## Pull Request Info Jira ticket: https://jira.mongodb.org/browse/DOCSP-39924 For each of the following SDK pages: - Define data model: add `Model Unstructured Data` section to define data model page - Mixed data property: update with support for holding lists & dicts, and add `Collections as Mixed` subsection **Kotlin (v2.0.0):** - [Define an Object Model - Kotlin SDK](https://preview-mongodbcbullinger.gatsbyjs.io/realm/docsp-39924-unstructured-data/sdk/kotlin/realm-database/schemas/define-realm-object-model/#model-unstructured-data) & [Supported Data Types - Kotlin SDK](https://preview-mongodbcbullinger.gatsbyjs.io/realm/docsp-39924-unstructured-data/sdk/kotlin/realm-database/schemas/supported-types/#realmany--mixed-) **.NET (v12.22.0):** - [Define an Object Model - .NET SDK](https://preview-mongodbcbullinger.gatsbyjs.io/realm/docsp-39924-unstructured-data/sdk/dotnet/model-data/define-object-model/#model-unstructured-data) & [RealmValue - .NET SDK](https://preview-mongodbcbullinger.gatsbyjs.io/realm/docsp-39924-unstructured-data/sdk/dotnet/model-data/data-types/realm-value/) **Node.js (v12.9.0):** - [Define an Object Model - Node.js SDK](https://preview-mongodbcbullinger.gatsbyjs.io/realm/docsp-39924-unstructured-data/sdk/node/model-data/define-object-model/#model-unstructured-data) & [Mixed - Node.js SDK](https://preview-mongodbcbullinger.gatsbyjs.io/realm/docsp-39924-unstructured-data/sdk/node/model-data/data-types/mixed/#nested-collections-of-mixed) **React Native (v12.9.0):** - [Define an Object Model - React Native SDK](https://preview-mongodbcbullinger.gatsbyjs.io/realm/docsp-39924-unstructured-data/sdk/react-native/model-data/define-object-model/#model-unstructured-data) & [Mixed - React Native SDK](https://preview-mongodbcbullinger.gatsbyjs.io/realm/docsp-39924-unstructured-data/sdk/react-native/model-data/data-types/mixed/#nested-collections-of-mixed) **Swift (v10.15.0):** - [Define an Object Model - Swift SDK](https://preview-mongodbcbullinger.gatsbyjs.io/realm/docsp-39924-unstructured-data/sdk/swift/model-data/object-models/#model-unstructured-data) & [Supported Types - Swift SDK](https://preview-mongodbcbullinger.gatsbyjs.io/realm/docsp-39924-unstructured-data/sdk/react-native/model-data/data-types/mixed/) ## Release Notes **Kotlin SDK** - Realm > Model Data > Define an Object Model: Add new "Define Unstructured Data" section outlining support for *Collections as Mixed* feature. - Realm > Model Data > Supported Data Types: Update ``RealmAny`` section to indicate support for holding lists and dictionaries, and add new "Mixed Collections" section. **.NET SDK** - Model Data > Define an Object Model: Add new "Model Unstructured Data" section outlining support for *Collections as Mixed* feature. - Model Data > Supported Data Types > RealmValue: Update page to indicate support for holding lists and dictionaries, and add new "Mixed Collections" section. **Node.js SDK** - Model Data > Define an Object Model: Add new "Model Unstructured Data" section outlining support for *Collections as Mixed* feature. - Model Data > Supported Data Types > Mixed: Update page to indicate support for holding lists and dictionaries, and add new "Mixed Collections" section. **React Native SDK** - Model Data > Define an Object Model: Add new "Define Unstructured Data" section outlining support for *Collections as Mixed* feature. - Model Data > Supported Data Types > Mixed: Update page to indicate support for holding lists and dictionaries, and add new "Mixed Collections" section. **Swift SDK** - Model Data > Define an Object Model: Add new "Model Unstructured Data" section outlining support for *Collections as Mixed* feature. - Model Data > Supported Types: Update ``AnyRealmValue`` section to indicate support for holding lists and dictionaries, and add new "Mixed Collections" section.
1 parent 4419604 commit 1352bb4

File tree

15 files changed

+1106
-759
lines changed

15 files changed

+1106
-759
lines changed

source/sdk/dotnet/model-data/data-types/realm-value.txt

Lines changed: 31 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -10,29 +10,49 @@ RealmValue - .NET SDK
1010
:depth: 2
1111
:class: singlecol
1212

13-
.. versionadded:: 10.2.0
13+
.. versionchanged:: 12.2.0
1414

15-
Overview
16-
--------
15+
``RealmValue`` can hold lists and dictionaries of mixed data.
1716

18-
The ``RealmValue`` data type is a mixed data type, and can represent any
19-
other valid Realm data type except a collection. You can create collections
20-
(lists, sets, and dictionaries) of type ``RealmValue``, but a ``RealmValue``
21-
itself cannot be a collection:
17+
The ``RealmValue`` data type is a mixed data type, and can represent any
18+
other valid Realm data type except an embedded object or a set. You can create collections
19+
(lists, sets, and dictionaries) of type ``RealmValue``:
2220

2321
.. literalinclude:: /examples/generated/dotnet/DataTypesSectionExamples.snippet.realmValues.cs
2422
:language: csharp
2523

26-
.. note::
24+
.. note::
2725

2826
You cannot create a nullable ``RealmValue``. However, if you want a
29-
``RealmValue`` property to contain a null value, you can
27+
``RealmValue`` property to contain a null value, you can
3028
use the special ``RealmValue.Null`` property.
3129

32-
The following code demonstrates creating a ``RealmValue`` property in a class
33-
that inherits from ``IRealmObject`` and then setting and getting the value of
30+
Create a RealmValue Property
31+
----------------------------
32+
33+
The following code demonstrates creating a ``RealmValue`` property in a class
34+
that inherits from ``IRealmObject`` and then setting and getting the value of
3435
that property:
3536

3637
.. literalinclude:: /examples/generated/dotnet/DataTypesSectionExamples.snippet.realmvalue.cs
3738
:language: csharp
3839

40+
.. _dotnet-nested-collections-realm-value:
41+
42+
Collections as Mixed
43+
--------------------
44+
45+
In version 12.22.0 and later, a ``RealmValue`` data type can hold collections
46+
(a list or dictionary, but *not* a set) of ``RealmValue`` elements. You can use
47+
mixed collections to model unstructured or variable data. For more information,
48+
refer to :ref:`<dotnet-model-unstructured-data>`.
49+
50+
- You can nest mixed collections up to 100 levels.
51+
- You can query mixed collection properties and
52+
:ref:`register a listener for changes <dotnet-collection-notifications>`,
53+
as you would a normal collection.
54+
- You can find and update individual mixed collection elements
55+
- You *cannot* store sets or embedded objects in mixed collections.
56+
57+
To use mixed collections, define the ``RealmValue`` type property in your data model.
58+
Then, set the property as a list or dictionary.

source/sdk/dotnet/model-data/define-object-model.txt

Lines changed: 108 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -24,19 +24,19 @@ Realm classes are regular C# classes that define the Realm schema.
2424
Object Schema
2525
~~~~~~~~~~~~~
2626

27-
An **object schema** is a configuration object that defines the properties and
28-
relationships of a Realm object. Realm client
29-
applications define object schemas with the native class implementation in their
27+
An **object schema** is a configuration object that defines the properties and
28+
relationships of a Realm object. Realm client
29+
applications define object schemas with the native class implementation in their
3030
respective language using the Object Schema.
3131

3232
Object schemas specify constraints on object properties such as the data
33-
type of each property and whether or not a property is required. Schemas can
34-
also define :ref:`relationships <dotnet-client-relationships>` between object
33+
type of each property and whether or not a property is required. Schemas can
34+
also define :ref:`relationships <dotnet-client-relationships>` between object
3535
types in a realm.
3636

3737
Every App has a :ref:`App Services Schema <dotnet-realm-schema>`
3838
composed of a list of object schemas for each type of object that the
39-
realms in that application may contain. Realm guarantees that all
39+
realms in that application may contain. Realm guarantees that all
4040
objects in a realm conform to the
4141
schema for their object type and validates objects whenever they're
4242
created, modified, or deleted.
@@ -46,8 +46,8 @@ created, modified, or deleted.
4646

4747
Property Annotations
4848
--------------------
49-
Schema properties are standard C# properties on a ``RealmObject``. There are
50-
several property annotations that you can use to more finely define how a Realm
49+
Schema properties are standard C# properties on a ``RealmObject``. There are
50+
several property annotations that you can use to more finely define how a Realm
5151
handles a specific property.
5252

5353
.. _dotnet-primary-key-example:
@@ -68,20 +68,20 @@ create a primary key with any of the following types (or their nullable counterp
6868
- ``int``
6969
- ``long``
7070

71-
You may define a primary key on a **single property** for an
71+
You may define a primary key on a **single property** for an
7272
object type as part of the :ref:`object schema <dotnet-object-schema>`.
7373
Realm automatically indexes primary key properties, which
7474
allows you to efficiently read and modify objects based on their primary
75-
key.
75+
key.
7676

7777
If an object type has a primary key, then all objects of that type must
7878
include the primary key property with a value that is unique among
7979
objects of the same type in a realm.
8080

8181
.. note::
82-
82+
8383
Once you assign a property as a primary key, you cannot change it.
84-
84+
8585
The following example demonstrates how to designate a primary key in an object schema:
8686

8787
.. literalinclude:: /examples/generated/dotnet/Objects.snippet.primary-key.cs
@@ -98,9 +98,9 @@ The following example demonstrates how to designate a primary key in an object s
9898
Indexes
9999
~~~~~~~
100100

101-
**Indexes** significantly improve query times in a Realm. Without
102-
indexes, Realm scans every document in a collection to select the documents
103-
that match the given query. However, if an applicable index exists for a query,
101+
**Indexes** significantly improve query times in a Realm. Without
102+
indexes, Realm scans every document in a collection to select the documents
103+
that match the given query. However, if an applicable index exists for a query,
104104
Realm uses the index to limit the number of documents that it must inspect.
105105

106106
You can index properties with the following types:
@@ -124,38 +124,38 @@ You can index properties with the following types:
124124
by your realm file. Each index entry is a minimum of 12 bytes.
125125

126126
To index a property, use the :dotnet-sdk:`Indexed <reference/Realms.IndexedAttribute.html>`
127-
attribute. With the ``Indexed`` attribute, you can specify the type of index
128-
on the property by using the :dotnet-sdk:`IndexType <reference/Realms.IndexType.html>`
129-
enum. In the following example, we have a default ("General") index on the ``Name``
127+
attribute. With the ``Indexed`` attribute, you can specify the type of index
128+
on the property by using the :dotnet-sdk:`IndexType <reference/Realms.IndexType.html>`
129+
enum. In the following example, we have a default ("General") index on the ``Name``
130130
property:
131131

132132
.. literalinclude:: /examples/generated/dotnet/Objects.snippet.index.cs
133133
:language: csharp
134134
:emphasize-lines: 3-4
135135

136-
.. note::
136+
.. note::
137137

138-
When you create an index, you are creating it on the local realm and not
139-
on an Atlas collection. If you need to query an Atlas collection directly
140-
and want to improve performance, refer to
138+
When you create an index, you are creating it on the local realm and not
139+
on an Atlas collection. If you need to query an Atlas collection directly
140+
and want to improve performance, refer to
141141
`Create, View, Drop, and Hide Indexes <https://www.mongodb.com/docs/atlas/atlas-ui/indexes/>`__.
142-
142+
143143
.. _dotnet-fts-indexes:
144144

145145
Full-Text Search Indexes
146146
~~~~~~~~~~~~~~~~~~~~~~~~
147147

148-
In addition to standard indexes, Realm also supports Full-Text Search (FTS) indexes
149-
on ``string`` properties. While you can query a string field with or without a
150-
standard index, an FTS index enables searching for multiple words and phrases and
151-
excluding others.
148+
In addition to standard indexes, Realm also supports Full-Text Search (FTS) indexes
149+
on ``string`` properties. While you can query a string field with or without a
150+
standard index, an FTS index enables searching for multiple words and phrases and
151+
excluding others.
152152

153-
For more information on querying full-text indexes, see :ref:`Full Text Search
153+
For more information on querying full-text indexes, see :ref:`Full Text Search
154154
(LINQ) <dotnet-linq-fts>` and :ref:`Full Text Search (RQL) <dotnet-rql-fts>`.
155155

156156
To index an FTS property, use the :dotnet-sdk:`Indexed <reference/Realms.IndexedAttribute.html>`
157-
attribute with the :dotnet-sdk:`IndexType.FullText <reference/Realms.IndexType.html>`
158-
enum. In the following example, we have a ``FullText`` index on the
157+
attribute with the :dotnet-sdk:`IndexType.FullText <reference/Realms.IndexType.html>`
158+
enum. In the following example, we have a ``FullText`` index on the
159159
``Biography`` property:
160160

161161
.. literalinclude:: /examples/generated/dotnet/Objects.snippet.index.cs
@@ -167,11 +167,11 @@ enum. In the following example, we have a ``FullText`` index on the
167167
Default Field Values
168168
~~~~~~~~~~~~~~~~~~~~
169169

170-
You can use the built-in language features to assign a default value to a property.
171-
In C#, you can assign a default value on primitives in the property declaration.
172-
You cannot set a default value on a collection, except to set it to ``null!``.
173-
Even if you set a collection to ``null!``, collections are always initialized on
174-
first access, so will never be null.
170+
You can use the built-in language features to assign a default value to a property.
171+
In C#, you can assign a default value on primitives in the property declaration.
172+
You cannot set a default value on a collection, except to set it to ``null!``.
173+
Even if you set a collection to ``null!``, collections are always initialized on
174+
first access, so will never be null.
175175

176176
.. literalinclude:: /examples/generated/dotnet/Objects.snippet.default.cs
177177
:language: csharp
@@ -190,7 +190,7 @@ Ignore a Property
190190
~~~~~~~~~~~~~~~~~
191191

192192
If you don't want to save a property in your model to a realm, you can
193-
ignore that property. A property is ignored by default if it is not autoimplemented or
193+
ignore that property. A property is ignored by default if it is not autoimplemented or
194194
does not have a setter.
195195

196196
Ignore a property from a Realm object model with the
@@ -249,24 +249,66 @@ attribute to rename a class:
249249
Custom Setters
250250
~~~~~~~~~~~~~~
251251

252-
Realm will not store a property with a custom setter. To use a custom setter,
253-
store the property value in a private property and then
254-
map that value to a public property with the custom setter. Realm will store the
252+
Realm will not store a property with a custom setter. To use a custom setter,
253+
store the property value in a private property and then
254+
map that value to a public property with the custom setter. Realm will store the
255255
private property, while you modify its value via the public property.
256-
In the following code, the private ``email`` property is stored in the realm,
256+
In the following code, the private ``email`` property is stored in the realm,
257257
but the public ``Email`` property, which provides validation, is not persisted:
258258

259259
.. literalinclude:: /examples/generated/dotnet/Objects.snippet.custom-setter.cs
260260
:language: csharp
261261

262+
.. _dotnet-model-unstructured-data:
263+
264+
Define Unstructured Data
265+
-----------------------
266+
267+
.. versionadded:: 12.22.0
268+
269+
Starting in SDK version 12.22.0, you can store :ref:`collections of mixed data <dotnet-nested-collections-realm-value>`
270+
within a ``RealmValue`` property. You can use this feature to model complex data
271+
structures, such as JSON or MongoDB documents, without having to define a
272+
strict data model.
273+
274+
**Unstructured data** is data that doesn't easily conform to an expected
275+
schema, making it difficult or impractical to model to individual
276+
data classes. For example, your app might have highly variable data or dynamic
277+
data whose structure is unknown at runtime.
278+
279+
Storing collections in a mixed property offers flexibility without sacrificing
280+
functionality, including performant synchronization when using Device Sync. And
281+
you can work with them the same way you would a non-mixed
282+
collection:
283+
284+
- You can nest mixed collections up to 100 levels.
285+
- You can query on and :ref:`react to changes
286+
<dotnet-collection-notifications>` on mixed collections.
287+
- You can find and update individual mixed collection elements.
288+
289+
However, storing data in mixed collections is less performant than using a structured
290+
schema or serializing JSON blobs into a single string property.
291+
292+
To model unstructured data in your app, define the appropriate properties in
293+
your schema as :ref:`RealmValue <realmvalue>` types. You can then
294+
set these ``RealmValue`` properties as a :ref:`list <dotnet-property-lists>` or a
295+
:ref:`dictionary <dotnet-client-dictionaries>` of ``RealmValue`` elements.
296+
Note that ``RealmValue`` *cannot* represent a set or an embedded
297+
object.
298+
299+
.. tip::
300+
301+
- Use a map of mixed data types when the type is unknown but each value will have a unique identifier.
302+
- Use a list of mixed data types when the type is unknown but the order of objects is meaningful.
303+
262304
.. _dotnet-omit-classes-from-schema:
263305
.. _dotnet-provide-subset-classes-schema:
264306

265307
Omit Classes from your Realm Schema
266308
-----------------------------------
267309

268310
By default, your application's Realm Schema includes all
269-
classes that implement ``IRealmObject`` or ``IEmbeddedObject``.
311+
classes that implement ``IRealmObject`` or ``IEmbeddedObject``.
270312
If you only want to include a subset of these classes in your Realm
271313
Schema, you can update your configuration to include the specific classes you want:
272314

@@ -281,41 +323,41 @@ Schema, you can update your configuration to include the specific classes you wa
281323
Required and Optional Properties
282324
--------------------------------
283325

284-
In C#, value types, such as ``int`` and ``bool``, are implicitly non-nullable.
285-
However, they can be made optional by using the question mark (``?``) `notation
326+
In C#, value types, such as ``int`` and ``bool``, are implicitly non-nullable.
327+
However, they can be made optional by using the question mark (``?``) `notation
286328
<https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/builtin-types/nullable-value-types>`__.
287329

288-
Beginning with C# 8.0, nullable reference types were introduced. If your project
289-
is using C# 8.0 or later, you can also declare reference types, such as ``string``
290-
and ``byte[]``, as nullable with ``?``.
330+
Beginning with C# 8.0, nullable reference types were introduced. If your project
331+
is using C# 8.0 or later, you can also declare reference types, such as ``string``
332+
and ``byte[]``, as nullable with ``?``.
291333

292-
.. note::
334+
.. note::
293335

294-
Beginning with .NET 6.0, the nullable context is enabled by default for new
295-
projects. For older projects, you can manually enable it. For more information,
336+
Beginning with .NET 6.0, the nullable context is enabled by default for new
337+
projects. For older projects, you can manually enable it. For more information,
296338
refer to `<https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/builtin-types/nullable-reference-types#setting-the-nullable-context>`__.
297339

298-
The Realm .NET SDK fully supports the nullable-aware context and uses nullability
299-
to determine whether a property is required or optional. The SDK has the
340+
The Realm .NET SDK fully supports the nullable-aware context and uses nullability
341+
to determine whether a property is required or optional. The SDK has the
300342
following rules:
301343

302344
- Realm assumes that both value- and reference-type properties are required if
303-
you do not designate them as nullable. If you designate them as nullable
345+
you do not designate them as nullable. If you designate them as nullable
304346
by using ``?``, Realm considers them optional.
305347

306348
- You must declare properties that are Realm object types as nullable.
307349

308-
- You cannot declare collections (list, sets, backlinks, and dictionaries) as
350+
- You cannot declare collections (list, sets, backlinks, and dictionaries) as
309351
nullable, but their parameters may be nullable according to the following rules:
310352

311-
- For all types of collections, if the parameters are primitives
353+
- For all types of collections, if the parameters are primitives
312354
(value- or reference-types), they can be required or nullable.
313-
314-
- For lists, sets, and backlinks, if the parameters are Realm objects, they
355+
356+
- For lists, sets, and backlinks, if the parameters are Realm objects, they
315357
**cannot** be nullable.
316358

317-
- For dictionaries with a value type of Realm object, you **must** declare
318-
the value type parameter as nullable.
359+
- For dictionaries with a value type of Realm object, you **must** declare
360+
the value type parameter as nullable.
319361

320362
The following code snippet demonstrates these rules:
321363

@@ -324,17 +366,18 @@ The following code snippet demonstrates these rules:
324366

325367
.. note::
326368

327-
If you are using the older schema type definition (your classes derive from
328-
the ``RealmObject`` base class), or you do not have nullability enabled, you
329-
will need to use the
330-
:dotnet-sdk:`[Required] <reference/Realms.RequiredAttribute.html>` attribute
369+
If you are using the older schema type definition (your classes derive from
370+
the ``RealmObject`` base class), or you do not have nullability enabled, you
371+
will need to use the
372+
:dotnet-sdk:`[Required] <reference/Realms.RequiredAttribute.html>` attribute
331373
for any required ``string`` and ``byte[]`` property.
332374

333375
Ignoring Nullability
334376
~~~~~~~~~~~~~~~~~~~~
335-
You may prefer to have more flexibility in defining the nullability of properties
336-
in your Realm objects. You can do so by setting ``realm.ignore_objects_nullability = true``
377+
You may prefer to have more flexibility in defining the nullability of properties
378+
in your Realm objects. You can do so by setting ``realm.ignore_objects_nullability = true``
337379
in a `global configuration file <https://learn.microsoft.com/en-us/dotnet/fundamentals/code-analysis/configuration-files>`__.
338380

339381
If you enable ``realm.ignore_objects_nullability``, nullability annotations
340-
will be ignored on Realm object properties, including collections of Realm objects.
382+
will be ignored on Realm object properties, including collections of Realm
383+
objects.

0 commit comments

Comments
 (0)