Database Reference
In-Depth Information
Limits on document size
BSON documents in MongoDB v2.0 are limited to 16 MB in size. 12 The limit exists for
two related reasons. First, it's there to prevent developers from creating ungainly data
models. Though poor data models are still possible with this limit, the 16 MB limit
helps discourage documents with especially deep levels of nesting, which is a common
data modeling error made by novice MongoDB users. Deeply nested documents are
difficult to work with; it's often better to expand the nested documents into separate
collections.
The second reason for the 16 MB limit is performance-related. On the server side,
querying a large document requires that the document be copied into a buffer before
being sent to the client. This copying can get expensive, especially (as is often the
case) when the client doesn't need the entire document. 13 In addition, once sent,
there's the work of transporting the document across the network and then deserializ-
ing it on the driver side. This can become especially costly if large batches of multi-
megabyte documents are being requested at once.
The upshot is that if you have especially large objects, you're probably better off
splitting them up, modifying your data model, and using an extra collection or two. If
you're simply storing large binary objects, like images or videos, that's a slightly differ-
ent case. See appendix C for techniques on handling large binary objects.
B ULK INSERTS
As soon as you have valid documents, the process of inserting them is straightforward.
Most of the relevant details about inserting documents, including object ID genera-
tion, how inserts work on the network layer, and safe mode, were covered in chapter 3.
But one final feature, bulk inserts, is worth discussing here.
All of the drivers make it possible to insert multiple documents at once. This can
be extremely handy if you're inserting lots of data, as in an initial bulk import or a
migration from another database system. Recall the earlier example of inserting 40
documents into the user.actions collection. If you look at the code, you'll see that
you're inserting just one document at a time. With the following code, you build an
array of 40 documents in advance and then pass the entire array of documents to the
insert method:
docs = (0..40).map do |n|
{ :username => "kbanker",
:action_code => rand(5),
:time => Time.now.utc,
:n => n
}
end
12
The number has varied by server version and is continually increasing. To see the limit for your server version,
run db.ismaster from the shell, and examine the maxBsonObjectSize field. If you can't find this field,
then the limit is 4 MB (and you're using a very old version of MongoDB).
13
As you'll see in the next chapter, you can always specify which fields of a document to return in a query to
limit response size. If you're doing this frequently, it may be worth reevaluating your data model.
Search WWH ::




Custom Search