Does a collection's size count towards the 16 MB BSON limit?

If I hypothetically have a User object that has nothing besides a List, does the number of elements in the list count towards the 16 MB BSON limit on the User record? So, if I have 1 million Todo in the List, is the size of the User BSON 1 million * some_constant or is it really small still?

Thanks much,

Hey Jon,

The 16MB limit is for the whole document/object.

So if you have a million todos in the list, the size will be 1 million * size of each todo object. It is stored together with the rest of the fields in the User object.

Please let me know if you have any other questions


Even if Todo is a separate collection?

To be more specific, I’m talking about something like this (RealmSwift code)

import Foundation
import RealmSwift

class User: Object, ObjectKeyIdentifiable {
    @Persisted(primaryKey: true) var _id: ObjectId
    @Persisted var todos: RealmSwift.List<Todo>

class Todo: Object, ObjectKeyIdentifiable {
    @Persisted(primaryKey: true) var _id: ObjectId
    @Persisted(originProperty: "todos") var users: LinkingObjects<User>

It is 16MB per Document. So if each todo item is a separate document they can each be 16MB. You can have x number of documents and EACH one can be up to 16mb in size.

For example, if you have 1 million documents like below each of those 1 million would need to be 16MB or less.

user_id: 1234
todo: [task1, task2,task3, etc]

Keep in mind however that the primary keys of the linked objects will be included in the parent document (that is how they are represented in your Atlas collections), so a hypothetical 1 million linked objects will still bloat the document size of the parent object.

1 Like

Thank you Kiro Morkos, that’s what I was verifying. I wasn’t sure if there was something like an SQL join table under the hood, I don’t know much about MongoDB.

Apologies Jon, I misinterpreted your question.

You’re correct, the size of the full Todo will not count toward the size of the User document. The list of Todos on the user document will only contain a list of primary keys that reference the individual Todo documents. In this case, it will be a list of the ObjectIds only. These ObjectIds will count toward the size of the user document.

You should be able to confirm this behavior by inspecting the user objects on the MongoDB collection directly and see that todos on documents contains only a list of ObjectIds.

1 Like

No problem, I could’ve been more clear. Thanks for responding so quickly :slight_smile:

This topic was automatically closed 5 days after the last reply. New replies are no longer allowed.