Database Reference
In-Depth Information
▪ MongoDB documents have a hard size limit of 16 MB.
The problem with taking up too much RAM is that RAM is usually the most critical resource
on a MongoDB server. In particular, a MongoDB database caches frequently accessed docu-
ments in RAM, and the larger those documents are, the fewer that will fit. The fewer docu-
mentsinRAM,themorelikelytheserveristopagefaulttoretrievedocuments,andultimately
page faults lead to random disk I/O.
In the case of our blogging platform, we may only wish to display the first three comments
by default when showing a blog entry. Storing all 500 comments along with the entry, then, is
simply wasting that RAM in most cases.
The second point, that growing documents need to be copied, has to do with update perform-
ance. As you append to the embedded comments array, eventually MongoDB is going to need
to move the document to an area with more space available. This movement, when it happens,
significantly slows update performance.
The final point, about the size limit of MongoDB documents, means that if you have a poten-
tially unbounded arity in your relationship, it is possible to run out of space entirely, prevent-
ing new comments from being posted on an entry. Although this is something to be aware of,
youwill usually runintoproblems duetomemory pressure anddocument copyingwell before
you reach the 16 MB size limit.
Many-to-Many Relationships
One final factor that weighs in favor of using document references is the case of many-to-
many or M:N relationships. For instance, suppose we have an ecommerce system storing
products and categories. Each product may be in multiple categories, and each category may
contain multiple products. One approach we could use would be to mimic a relational many-
to-many schema and use a “join collection”:
// db.product schema
{ "_id" : "My Product" , ... }
// db.category schema
{ "_id" : "My Category" , ... }
// db.product_category schema
{ "_id" : ObjectId (...),
"product_id" : "My Product" ,
"category_id" : "My Category" }
Search WWH ::




Custom Search