Subscribing to Atlas view

Hi. I have created a view of a collection via aggregation. I’m wondering how can I suscribe to such view in the swift SDK?
I’m trying to filter a subscription based on a query and it seems like views are a viable solution but I don’t know how to get that data in the mobile app. I want to avoid to subscribe to the whole collection (200k+ records) and filter locally.

Thanks

Device Sync does not support syncing on a view. The reason is that a view is read-only (cant be updated) and does not support change streams (which is how we react to changes in the database). I would recommend adding a field to the document that allows you to have the client query on that subset of data.

In the simplest case, you could think of this as adding a field like “inView: bool” to the documents and then you can sync on the subscription “inView == true”

Best,
Tyler

Thanks, but then I can’t apply the logic per user.
I was thinking then that I could use access rules and apply the logic there. Would that work?
I’m just trying to apply something like

if id IN [array of objectId inside another collection]

But that doesn’t seem to work in swift subscriptions

Hi,

That should definitely work from Swift, so I would be curious to know what the issue is.

You are correct that my solution was not the scalable approach for many users (though neither would using a view unless you had a view-per-client which I think would have been a configuration challenge).

I would strongly advise you think about if you can either store data on the document indicating which users care about that data so that it can be efficiently queried. This is standard for most applications, and querying on a list of _ids that can grow endlessly will lead to problems down the line.

Can you either store a field like user_ids on the documents (the aggregation can update that) and then sync on documents where user_ids contain the current user_id? Some system that uses the data (or some value in custom user data) to do this will be a much better option for you.

I would recommend reading through this: https://www.mongodb.com/docs/atlas/app-services/sync/app-builder/device-sync-permissions-guide/

Best,
Tyler

I will take that into consideration, Thanks a lot.

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