Realm Sync: How to avoid storing all documents on mobile device?

Hello, I am building a react native app using Realm with MongoDB Device sync (flexible sync). I want to know how can I limit the number of documents on the mobile client side without deleting them from the database. For example, if I have a chat app with 1 million messages inside a chat. I want all of them to be persisted on to MongoDB Atlas, but only the last 100 to be present on the mobile device. Basically, the mobile will be pushing documents to the cloud, but will only see a subset of them - last N count or only the documents from the past month, etc. Should I be using Rules (I already have rules set up and am not sure if this is combinable with them) or should I be using a Realm Query? The point is to avoid storing all of the documents on the mobile device. Could you also provide a simple example?

Hi, you should lean on “Subscriptions” in flexible sync for this. Unfortunately, we don’t allow subscriptions on a “limit” due to technical limitations (we would constantly need to re-evaluate the query changes to one object can affect if another object moves in/out of view). Most of our users who want to mimic the limit behavior instead use a time-based query. So you can add a time field to each message and subscribe to all queries that are from the last 7 days let’s say. Then have your app periodically refresh the subscription with a new time whenever it is opened or after some amount of time.

This topic was automatically closed 5 days after the last reply. New replies are no longer allowed.