Modeling sub-collections in MongoDB Realm Sync

I’m new to MongoDB as well as to MongoDB Realm Sync. I was following the Realm Sync tutorial and Realm data model docs, but I wanted to learn more so I tweaked the Atlas collection structure as follows.

Projects > Tasks // i.e. tasks is a sub-collection in each project.

What I don’t know is how to come up with Realm Sync Schema which can support sub-collections. The best I came up with is a Schema where Task s are modelled as an array within the Project . But, I’m worried that this can hit the 4MB (although a lot!) document limit for projects with a lot of the tasks.

Hi @siddharth_kamaria, welcome to the community!

First of all, could you clarify what you mean by “sub-collection” and what you’re trying to achieve with them?

You’re correct that embedding Tasks within Projects could cause an issue if you had lots of large tasks.

There are some design schema patterns that can be used (e.g. storing the 20 tasks with the closest due date in the Project document and then the rest each having their own document), but it really depends on your access patterns and what you’re trying to achieve.

2 Likes

Hi Andrew,

After posting the video I came across your RChat blog posts and video which clarified a lot of the doubts that I had. Kudos to the team and you for hosting these sessions and writing blog posts!

By sub-collection I was referring to nested documents in MongoDB Atlas. I’ll enlist the route that I ended up taking.

  1. I created a Project model on user=\(user.id) partition. This allows all projects to be synced when a user logs in.
  2. I created a Task as a top level model with _id and _parition fields. I set the partition to project=\(project.id) for tasks belonging to a specific project.
  3. Assigning a project as the partition key allows me to share the project with other users. I just have to create a new Project document for each user it is shared with. This is very similar to the Chatster object if you are following the RChat example.

As for having the top N due tasks into the Project object is a good idea worth exploring. I would love to see more content on modelling Realm partitions, which IMHO is the most difficult and crucial part in getting the Realm Sync right.

2 Likes