BlogAtlas Vector Search voted most loved vector database in 2024 Retool State of AI reportLearn more >>
MongoDB Developer
Sign in to follow topics
MongoDB Developer Centerchevron-right
Developer Topicschevron-right

Model Unstructured Data: More Flexibility for Our Developers

Claus Rørbech4 min read • Published Jul 01, 2024 • Updated Jul 09, 2024
AtlasApp ServicesKotlin
Facebook Icontwitter iconlinkedin icon
Rate this article
With Atlas Device Sync, developers can craft local-first applications featuring real-time collaborative capabilities with other users and seamless data syncing with MongoDB Atlas. This ability to abstract away the complexities of synchronization logic is a substantial benefit for developers.
As we continue working to improve the developer experience, our teams have concentrated on lessening the pain points surrounding data modeling — specifically, unstructured data. In cases where you might not know your application’s data model at runtime or you’re working with proof of concepts, flexibility with data schemas is paramount. Ordinarily, you would have to use workarounds to overcome the limitations of unstructured data.
Until now. Enter our enhanced mixed data type, which can hold collections of mixed data! The ability to create what we’re referring to as collections as mixed introduces a powerful mechanism to model unstructured data and help our developers be even more productive with Atlas Device Sync and Atlas Device SDK!
Some scenarios where you would benefit from this feature:
  • You have a highly variable data structure across a number of entities that would be too tedious or impractical to model to individual data classes.
  • You collect and synchronize data from unknown sources through client applications, and you want to interpret them on the client device and to sync updates efficiently.
  • You want to manage remote devices with slightly varying capabilities by pushing out dynamically shaped configurations from a single central source.

What's so exciting about storing collections in mixed types?

Previously, you might have stored any unstructured or variable data in other data formats, such as JSON as raw strings. While this is a valid workaround, it also came with some unfortunate consequences: It rendered the mobile app unable to interpret that data without parsing it, you weren’t able to query on the properties within the data, and Device Sync had to transfer the full dataset as stringified data instead of only synchronizing individual updates.
With this enhanced mixed data type, you can store data with an unknown schema without sacrificing functionality. You also get the performant synchronization of that unstructured data between Atlas Device Sync clients and MongoDB Atlas. That’s because Device Sync only transfers the actual updates from the data structure and applies the usual rules when merging changes from multiple sources.

Example usage

Before we demonstrate how this new feature works, let’s provide some terminology in the context of Device Sync and Atlas Device SDK:
  • A collection is an object that contains zero or more instances of one type. All objects in a collection are of the same type. Collection types can be lists, sets, or dictionaries.
  • A mixed data type is a property type that can hold any valid data type (e.g., integer, objectID, string, object) except a collection — until now.
Now that you can store collections in a mixed property, you can easily model unstructured data by simply defining a mixed data type somewhere in your otherwise strictly typed schema. You can then use that mixed property to store your dynamic structure:
Atlas device sync schema: JSON Schema view in Atlas showing the fields and their data types
Kotlin model definition:
This allows you to construct entities that have strongly typed properties (id and name) and the dynamic data without a predefined structure (configuration), as shown below:
Mixed data in action
You can check out the above sample Kotlin app in our samples repository.

Querying unstructured data

The client-side Realm Query Language (RQL) lets you query for objects based on the content of its properties, including collections in mixed properties:
You can check out the full features of Realm Query Language in our RQL documentation.
The nested structures can similarly be queried on the Atlas collection using the MongoDB Query API:

Limitations and next steps

Because this new feature changes what you can represent in mixed fields, there are some limitations:
Additional considerations to keep in mind when using the enhanced mixed data type:
  • Mixed fields can now hold list and dictionary collections. This feature does not include support for sets or embedded objects in a mixed property.
  • There is a total maximum nesting level of 100.
  • When configuring Device Sync, you can subscribe to queries based on mixed fields and on lists contained in mixed fields. However, you cannot create subscriptions for queries on dictionaries contained in mixed fields.
  • Due to the dynamic nature of the mixed data types, the SDK requires an inspection of values, resulting in a small performance penalty compared to typed properties.
We are working hard to improve the flexibility of Atlas Device SDKs and Atlas Device Sync, and we hope this feature will make your development experience when modeling unstructured data much smoother. As part of this continued effort, we’re also focused on making it easier to change schemas in Production applications and better supporting unstructured data in a way that’s more familiar to developers with MongoDB experience.
Head on over to our community forums and let us know what you think! We look forward to your feedback.
Ready to get started now? Register for Atlas to connect to Atlas Device Sync, a fully-managed mobile backend as a service. Leverage out-of-the-box infrastructure, data synchronization capabilities, network handling, and much more to quickly launch enterprise-grade mobile apps. Finally, let us know what you think and get involved in our forums. See you there!
Top Comments in Forums
There are no comments on this article yet.
Start the Conversation

Facebook Icontwitter iconlinkedin icon
Rate this article

Deploying MongoDB Atlas With Terraform with Azure

Jun 18, 2024 | 7 min read

Taking RAG to Production with the MongoDB Documentation AI Chatbot

Jul 09, 2024 | 11 min read

A Decisioning Framework for MongoDB $regex and $text vs Atlas Search

May 30, 2023 | 5 min read

How to work with Johns Hopkins University COVID-19 Data in MongoDB Atlas

Nov 16, 2023 | 8 min read
Table of Contents