Private and public documents

Hi @Green,

Nice to hear from you again.

This exact problem is what Realm and Realm Rules are coming to solve where you can define your sync and read/write rules. However, to leverage this you need to use MongoDB Atlas and Ream applications which I very much encourage you.

https://www.mongodb.com/how-to/realm-partitioning-strategies/

However, if you wish to use MongoDB you should make sure that the queries add the correct filtering criteria.

What I think you can do is to use a $unionWith or 2 queries/aggregations to get the data. You will index the {userId 1 , isPublic : 1} of course and isPublic separately (or even hold public data in antoher collection).

Instead of making one query with an OR maybe run 2 or a union of:

  1. The user private data filtering only on user and isPublic=false.
  2. The public data.

If you wish the result set to come back in a natural or _id order you can add a sort stage in the end (adding the sorting field _id to the index last part)

Let me know what you think,

Thanks
Pavel