Yet another approach, used less frequently, is to use a DBRef. Technically speaking, there is actually no difference between creating a DBRef as compared to simply adding a document reference (see the previous sub-topic). In both cases, an additional lookup is required. The main difference between using a DBRef compared to using a document reference is that the DBRef is a uniform, formalized way of stating the reference.
DBRef is actually not a data type in and of itself. Rather, it's a formal JSON structure that completely identifies the document in the target collection. Here are the fields required to insert a properly recognized DBRef:
- $ref: The name of the target collection
- $id: The value of the _id field in the document to be referenced in the target collection
In addition, a third field, $db, containing the name of the target collection's database, can also be added when defining a DBRef, however, this is not supported by all programming...