EventGet 50% off your ticket to MongoDB.local NYC on May 2. Use code Web50!Learn more >>
MongoDB Developer
Realm
plus
Sign in to follow topics
MongoDB Developer Centerchevron-right
Developer Topicschevron-right
Productschevron-right
Realmchevron-right

New Realm Data Types: Dictionaries/Maps, Sets, Mixed, and UUIDs

Ian Ward17 min read • Published Jan 13, 2022 • Updated Oct 19, 2022
Realm
Facebook Icontwitter iconlinkedin icon
Rate this announcement
star-empty
star-empty
star-empty
star-empty
star-empty

TL;DR

Starting with Realm Javascript 10.5, Realm Cocoa 10.8, Realm .NET 10.2, and Realm Java 10.6, developers will be able persist and query new language specific data types in the Realm Database. These include Dictionaries/Maps, Sets, a Mixed type, and UUIDs.

Introduction

We're excited to announce that the Realm SDK team has shipped four new data types for the Realm Mobile Database. This work – prioritized in response to community requests – continues to make using the Realm SDKs an intuitive, idiomatic experience for developers. It eliminates even more boilerplate code from your codebase, and brings the data layer closer to your app's source code.
These new types make it simple to model flexible data in Realm, and easier to work across Realm and MongoDB Atlas. Mobile developers who are building with Realm and MongoDB Realm Sync can leverage the flexibility of MongoDB's data structure in their offline-first mobile applications.
Read on to learn more about each of the four new data types we've released, and see examples of when and how to use them in your data modeling:
Build better mobile apps with Atlas Device Sync: Atlas Device Sync is a fully-managed mobile backend-as-a-service. Leverage out-of-the-box infrastructure, data synchronization capabilities, built-in network handling, and much more to quickly launch enterprise-grade mobile apps. Get started now by build: Deploy Sample for Free!

Dictionaries/Maps

Dictionaries/maps allow developers to store data in arbitrary key-value pairs. They're used when a developer wants to add flexibility to data models that may evolve over time, or handle unstructured data from a remote endpoint. They also enable a mobile developer to store unique key-value pairs without needing to check the data for uniqueness before insertion.
Both Dictionaries and Maps can be useful when working with REST APIs, where extra data may be returned that's not defined in a mobile app's schema. Mobile developers who need to future-proof their schema against rapidly changing feature requirements and future product iterations will also find it useful to work with these new data types in Realm.
Consider a gaming app that has multiple games within it, and a single Player class. The developer building the app knows that future releases will need to enable new functionality, like a view of player statistics and a leaderboard. But the Player can serve in different roles for each available game. This makes defining a strict structure for player statistics difficult.
With Dictionary/Map data types, the developer can place a gameplayStats field on the Player class as a dictionary. Using this dictionary, it's simple to display a screen that shows the player's most common roles, the games they've competed in, and any relevant statistics that the developer wants to include on the leaderboard. After the leaderboard has been released and iterated on, the developer can look to migrate their Dictionary to a more formally structured class as part of a formal feature.
Kotlin
Swift
JavaScript
C#

Mixed

Realm's Mixed type allows any Realm primitive type to be stored in the database, helping developers when strict type-safety isn't appropriate. Developers may find this useful when dealing with data they don't have total control over – like receiving data and values from a third-party API. Mixed data types are also useful when dealing with legacy states that were stored with the incorrect types. Converting the type could break other APIs and create considerable work. With Mixed types, developers can avoid this difficulty and save hours of time.
We believe Mixed data types will be especially valuable for users who want to sync data between Realm and MongoDB Atlas. MongoDB's document-based data model allows a single field to support many types across documents. For users syncing data between Realm and Atlas, the new Mixed type allows developers to persist data of any valid Realm primitive type, or any Realm Object class reference. Developers don't risk crashing their app because a field value violated type-safety rules in Realm.
Kotlin
Swift
JavaScript
C#

Sets

Sets allow developers to store an unordered array of unique values. This new data type in Realm opens up powerful querying and mutation capabilities with only a few lines of code.
With sets, you can compare data and quickly find matches. Sets in Realm have built-in methods for filtering and writing to a set that are unique to the type. Unique methods on the Set type include, isSubset(), contains(), intersects(), formIntersection, and formUnion(). Aggregation functions like min(), max(), avg(), and sum() can be used to find averages, sums, and similar.
Sets in Realm have the potential to eliminate hundreds of lines of gluecode. Consider an app that suggests expert speakers from different areas of study, who can address a variety of specific topics. The developer creates two classes for this use case: Expert and Topic. Each of these classes has a Set field of strings which defines the disciplines the user is an expert in, and the fields that the topic covers.
Sets will make the predicted queries easy for the developer to implement. An app user who is planning a Speaker Panel could see all experts who have knowledge of both "Autonomous Vehicles" and "City Planning." The application could also run a query that looks for experts in one or more of these disciples by using the built-in intersect method, and the user can use results to assemble a speaker panel.
Developers who are using MongoDB Realm Sync to keep data up-to-date between Realm and MongoDB Atlas are able to keep the semantics of a Set in place even when synchronizing data.
You can depend on the enforced uniqueness among the values of a Set. There's no need to check the array for a value match before performing an insertion, which is a common implementation pattern that any user of SQLite will be familiar with. The operations performed on Realm Set data types will be synced and translated to documents using the $addToSet group of operations on MongoDB, preserving uniqueness in arrays.
Kotlin
Swift
JavaScript
C#

UUIDs

The Realm SDKs also now support the ability to generate and persist Universally Unique Identifiers (UUIDs) natively. UUIDs are ubiquitous in app development as the most common type used for primary keys. As a 128-bit value, they have become the default for distributed storage of data in mobile to cloud application architectures - making collisions unheard of.
Previously, Realm developers would generate a UUID and then cast it as a string to store in Realm. But we saw an opportunity to eliminate repetitive code, and with the release of UUID data types, Realm comes one step closer to boilerplate-free code.
Like with the other new data types, the release of UUIDs also brings Realm's data types to parity with MongoDB. Now mobile application developers will be able to set UUIDs on both ends of their distributed datastore, and can rely on Realm Sync to perform the replication.
Kotlin
Swift
JavaScript
C#

Conclusion

From the beginning, Realm's engineering team has believed that the best line of code is the one a developer doesn't need to write. With the release of these unique types for mobile developers, we're eliminating the workarounds – the boilerplate code and negative impact on CPU and memory – that are commonly required with certain data structures. And we're doing it in a way that's idiomatic to the platform you're building on.
By making it simple to query, store, and sync your data, all in the format you need, we hope we've made it easier for you to focus on building your next great app.
Stay tuned by following @realm on Twitter.
Want to Ask a Question? Visit our Forums.
Want to be notified about upcoming Realm events, like talks on SwiftUI Best Practices or our new Kotlin Multiplatform SDK? Visit our Global Community Page.

Facebook Icontwitter iconlinkedin icon
Rate this announcement
star-empty
star-empty
star-empty
star-empty
star-empty
Related
Tutorial

Saving Data in Unity3D Using Realm


Dec 08, 2022 | 10 min read
Tutorial

Building an Android Emoji Garden on Jetpack Compose with Realm


Mar 06, 2023 | 24 min read
Tutorial

Online/Offline Data-Capable Cross-Platform Apps with MongoDB Atlas, Atlas Device SDKs and .NET MAUI


Nov 14, 2023 | 6 min read
News & Announcements

Realm Kotlin 0.4.1 Announcement


Mar 22, 2023 | 5 min read
Table of Contents
  • TL;DR