Databases Reference
In-Depth Information
Tip #17: Do not use database references
This tip is specifically about the special database reference subdocument
type, not references (as discussed in the previous chapter) in general.
Database references are normal subdocuments of the form {$id : identifier , $ref :
collectionName } (they can, optionally, also have a $db field for the database name).
They feel a bit relational: you're sort of referencing a document in another collection.
However, you're not really referencing another collection, this is just a normal subdo-
cument . It does absolutely nothing magical. MongoDB cannot dereference database
references on the fly; they are not a way of doing joins in MongoDB. They are just
subdocuments holding an _id and collection name. This means that, in order to dere-
ference them, you must query the database a second time.
If you are referencing a document but already know the collection, you might as well
save the space and store just the _id , not the _id and the collection name. A database
reference is a waste of space unless you do not know what collection the referenced
document will be in.
The only time I've heard of a database reference being used to good effect was for a
system that allowed users to comment on anything in the system. They had a com-
ments collection, and stored comments in that with references to nearly every other
collection and database in the system.
Tip #18: Don't use GridFS for small binary data
GridFS requires two queries: one to fetch a file's metadata and one to fetch its contents
( Figure 2-1 ). Thus, if you use GridFS to store small files, you are doubling the number
of queries that your application has to do. GridFS is basically a way of breaking up large
binary objects for storage in the database.
GridFS is for storing big data—larger than will fit in a single document. As a rule of
thumb, anything that is too big to load all at once on the client is probably not some-
thing you want to load all at once on the server. Therefore, anything you're going to
stream to a client is a good candidate for GridFS. Things that will be loaded all at once
on the client, such as images, sounds, or even small video clips, should generally just
be embedded in your main document.
Further reading:
How GridFS works
 
Search WWH ::




Custom Search