1

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?

BokuNoHeeeero
  • 243
  • 1
  • 2
  • 11

1 Answers1

1

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.

Dharmaraj
  • 47,845
  • 8
  • 52
  • 84
  • so, single object in collection is a document and this document can have max 16MB? – BokuNoHeeeero Apr 04 '22 at 18:43
  • @BokuNoHeeeero that's correct. And technically there isn't any limit on number of docs in a collection as long as you have enough storage – Dharmaraj Apr 04 '22 at 18:45
  • @BokuNoHeeeero yes. Though there are some differences in MongoDB and Firestore so you you might have different strucutures in both DBs. – Dharmaraj Apr 04 '22 at 18:49