MongoDB: Avoid using unbounded arrays in documents

MongoDB: Avoid using unbounded arrays in documents

···

on Wednesday, June 22nd, 2022, 12:00 AM

Hi everyone. Hope you are doing well. Today I will show you a scenario with the pros and cons that why you shouldn’t use unbound arrays in a MongoDB document. So without further ado let’s get started.

First, understand the scenario. Let’s assume we have a social media application where a user can follow multiple users and vice versa. So the initial MongoDB schema should look like this.

Initial schema

Here we should pay attention to 2 fields followers and followings. At the start of our application, we won’t have too much traffic as well as users. Let’s say we started with 10-20 k users. So at this scale, if a user follows another user it will store all data in that array of the schema. So documents stored in followers or followings array will have an average of 3 digits. In the worst-case scenario, it can go up to 4 digits. MongoDB can handle this much data for the time being. But the operation Can still be slow a little bit. This can be optimized. In this case, you can query for the followers in the following way.

Aggregation Framework

Get all followers for a user

But when the application grows in the number of users it can be hundreds of thousands of users even it can be millions of users. Then this initial user schema won’t work as expected on the other hand a single document in MongoDB can be max 16 MB. if a user can manage to get hundreds of thousands of followers the database will fail.

So to overcome this problem we can split the schema into two schemas. One is the User schema and another is Follow schema. These schemas should look like this.

Optimized schema

Now, this schema is more optimized and scalable than the previous one. In this model, a user can follow or can have millions of followers without any issue which was previously impossible. Now in this case you can’t use the previously used query to fetch all followers of a user. Now to get all followers of a user you need a query something like this.

Mongoose

Aggregation Framework

Don’t be confused about why I am filtering with the following field to get followers. just take a closer look on follow schema and give it a think you will understand easily.

That is all for today. Hope you find it useful. If you do please give it a clap and share it with your friends. If you like this kind of write-up consider following me. If you have any doubts you can always reach out to me.

Thank you for sticking this much.