Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

It is good to create collection for each users?

I have a question with the architectural ones. I create an application that collects a large amount of data from users and I have to save it somewhere. Saving all data from all users in one collection in mongodb very quickly will cause the allowable limit of 16MB of data in the collection to be reached. For this reason, I decided to create a new collection for each subsequent user of my application by creating a collection name like this:

const getCollectionName = (userId: string) => {
  return `${COLLECTIONS.DATA}_${userId}`
}

await mongoDb.createCollection(getCollectionName(userId));

I took this idea from my experience in working with firebase, where we can easily manage sub-collections for a given collection.

And now I have a question, is it a good idea to create a new collection for each user of my application separately, or are there some inconveniences resulting from this way of storing data that I have no idea about?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

In MongoDB, the maximum BSON document size is 16 megabytes and not *collection. As far as sub-collection goes, you can have nested collections in MongoDB (similar to Firestore) but that counts towards the 16 MB doc size limit.

Also checkout:


Is it a good idea to create a new collection for each user of my application separately,

While you can do this, you’ll have to create indexes for every collection whenever it is created. Storing data of all users in a single collection might be easier depending on the use case.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading