How to create a view from 2 or more collections that can be read in my realm flexible sync kotlin app?

Hi, I have a use case where I need to create a view of multiple collections so that I can read and page the data in my kotlin android app. None of the information I find online seems relevant to this environment. Could someone give me a link to some documentation on how I can create a view and use it in my app? We are using realm flexible sync and interacting with the data in a native android kotlin app. Any help is appreciated.

Hi, unfortunately we do not support syncing on a view in MongoDB as there are limitations on Views: https://www.mongodb.com/docs/manual/core/views/#views

The limitations that are important to sync are the fact that they are read-only and that they do not support change streams.

Is there a reason that you need to sync on a view and not just have it be a normal collection?

So I don’t really need any syncing or reaction to changes. The feature is fairly straightforward. I have multiple items in my app but they are very different bar a handful of basic fields “description/code” and whatnot. My users need to be able to search/filter the full catelog when doing a lookup. Because realm sync enforces a strict schema on the collections I cannot put them all in a single collection without having large amounts of useless columns per item type that will also clutter up the classes in the app.

I thought a view could be created between the items on common fields like I can do in mongo db for readonly/search purposes but are you saying this is not possible? Can I create this view locally and not have it sync?

I see this information from the link you gave me “MongoDB does not persist the view contents to disk. A view’s content is computed on-demand when a client queries the view.” This would be perfect. All I need is a view to read to the screen and I require no sync or interactions with the data. Can I do this using the realm sdk for kotlin using the data that is synced locally?

If you just need to query a view, I would recommend using GraphQL or the Data API. Additionally, the SDK’s have a Remote MongoDB Query functionality that returns documents and is capable of querying a view. https://www.mongodb.com/docs/realm/sdk/java/app-services/mongodb-remote-access/

I also need to be able to query this view while offline and was hoping to use the synced data

Got it. Unfortunately your only options are to either push this data into a normal collection (which should be pretty similar to the view iiuc). The other option is that you do not need to define schemas on all fields of the document. You can define a schema for just the fields you want to sync down and then you can query the data locally (but the different collections would be in different tables)

Thank you for the information. So to summarize

  • I can use the APIs that let me make online queries to views
  • I can create a new collection to act as the view then put my duplicated data into that
  • You can define a schema for just the fields you want to sync down and then you can query the data locally

Could you elaborate more on the 3rd item in that list?

Of course. You can define any subset of fields within your Realm Object Models or in the JSON Schema section of App Services. Only those fields will be synced down to the device. So if you only want to sync 2 or 3 fields that are in common to all of the collections, that is all you need to define and sync down.

Is there any plan to support views in future? I find it very limiting to working around the absence of this standard mongoDB feature.

“The limitations that are important to sync are the fact that they are read-only and that they do not support change streams.”

These limitations are perfectly acceptable in that I would be happy with a view that only reads from the locally stored synced data. No need to listen to changes etc

Its not currently on the road map at this time, but I would be curious to know more about your use case. If you don’t need to make writes, and the view is not changing, can you elaborate a bit more on why you need Device Sync?

It seems like your use case is a better for for using Realm purely as a local database (without sync) and using your own API or ours (Data API, Graph QL, Remote MongoDB) to retrieve the data on app startup (or on some cadence) and store it in a local realm to be used by the application.

Best,
Tyler

Hi Tyler. Thank you for the information. I do need device sync for the app I am creating. I would just have liked to be able to create local readonly views to save me having to alter the schema whenever I want to make slightly more complex queries. It would save duplication of data and costs but I have just made changes to my schema for now to get around my limitation.