Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How many BSON ObjectIds can a mongodb array field save

I am planning to save huge number of ids for foreignkey ids in this array. So, I am just checking what would be the max number of BSON::ObjectIds I can save in the field array. Lets say for example

department_ids: [BSON::OBjectId('57cf6d6e8315292136000001'), BSON::OBjectId('57cf6d6e8315292136000002') ...... ]
like image 253
user2624242 Avatar asked Sep 21 '16 16:09

user2624242


People also ask

What is the size of an ObjectID in MongoDB?

ObjectId values are 12 bytes in length, consisting of: A 4-byte timestamp, representing the ObjectId's creation, measured in seconds since the Unix epoch. A 5-byte random value generated once per process. This random value is unique to the machine and process.

How many documents can MongoDB handle?

MongoDB supports no more than 100 levels of nesting for BSON documents. Each object or array adds a level.

Can we override _id in MongoDB?

The _id field is immutable—that is, once a document exists in your MongoDB system, it has, by definition, been assigned an _id, and you cannot change or update its primary key. That said, _id can be overridden when you insert new documents, but by default it will be populated with an ObjectID.

How large can a MongoDB document be?

Document Size Limit The maximum BSON document size is 16 megabytes. The maximum document size helps ensure that a single document cannot use excessive amount of RAM or, during transmission, excessive amount of bandwidth. To store documents larger than the maximum size, MongoDB provides the GridFS API.


1 Answers

16MB is big enough to hold really large amount of ObjectId, ObjectIds aren't that heavy, they are 12 bytes and when you divide it by 16MB you get well beyond 1 million.

But in case you still aren't assured, you can benefit by the flexible schema design of Mongo and create one follow-up document to hold further arrays and store the _id of that document in the concerned document with a field named as "followedBy" or something.

downside is you will have to execute a follow-up query (or maybe not).

Hope that helps.

like image 82
Rahul Kumar Avatar answered Oct 03 '22 08:10

Rahul Kumar