Navigation
This version of the documentation is archived and no longer supported.

Database References

On this page

MongoDB does not support joins. In MongoDB some data is denormalized, or stored with related data in documents to remove the need for joins. However, in some cases it makes sense to store related information in separate documents, typically in different collections or databases.

MongoDB applications use one of two methods for relating documents:

  1. Manual references where you save the _id field of one document in another document as a reference. Then your application can run a second query to return the embedded data. These references are simple and sufficient for most use cases.

  2. DBRefs are references from one document to another using the value of the first document’s _id field collection, and optional database name. To resolve DBRefs, your application must perform additional queries to return the referenced documents. Many drivers have helper methods that form the query for the DBRef automatically. The drivers [1] do not automatically resolve DBRefs into documents.

    Use a DBRef when you need to embed documents from multiple collections in documents from one collection. DBRefs also provide a common format and type to represent these relationships among documents. The DBRef format provides common semantics for representing links between documents if your database must interact with multiple frameworks and tools.

Unless you have a compelling reason for using a DBRef, use manual references.

[1]Some community supported drivers may have alternate behavior and may resolve a DBRef into a document automatically.

Manual References

Background

Manual references refers to the practice of including one document’s _id field in another document. The application can then issue a second query to resolve the referenced fields as needed.

Process

Consider the following operation to insert two documents, using the _id field of the first document as a reference in the second document:

original_id = ObjectId()

db.places.insert({
    "_id": original_id,
    "name": "Broadway Center",
    "url": "bc.example.net"
})

db.people.insert({
    "name": "Erin",
    "places_id": original_id,
    "url":  "bc.example.net/Erin"
})

Then, when a query returns the document from the people collection you can, if needed, make a second query for the document referenced by the places_id field in the places collection.

Use

For nearly every case where you want to store a relationship between two documents, use manual references. The references are simple to create and your application can resolve references as needed.

The only limitation of manual linking is that these references do not convey the database and collection name. If you have documents in a single collection that relate to documents in more than one collection, you may need to consider using DBRefs.

DBRefs

Background

DBRefs are a convention for representing a document, rather than a specific reference “type.” They include the name of the collection, and in some cases the database, in addition to the value from the _id field.

Format

DBRefs have the following fields:

$ref

The $ref field holds the name of the collection where the referenced document resides.

$id

The $id field contains the value of the _id field in the referenced document.

$db

Optional.

Contains the name of the database where the referenced document resides.

Only some drivers support $db references.

Example

DBRef document would resemble the following:

{ "$ref" : <value>, "$id" : <value>, "$db" : <value> }

Consider a document from a collection that stored a DBRef in a creator field:

{
  "_id" : ObjectId("5126bbf64aed4daf9e2ab771"),
  // .. application fields
  "creator" : {
                  "$ref" : "creators",
                  "$id" : ObjectId("5126bc054aed4daf9e2ab772"),
                  "$db" : "users"
               }
}

The DBRef in this example, points to a document in the creators collection of the users database that has ObjectId("5126bc054aed4daf9e2ab772") in its _id field.

Note

The order of fields in the DBRef matters, and you must use the above sequence when using a DBRef.

Support

C++
The C++ driver contains no support for DBRefs. You can transverse references manually.
C#
The C# driver provides access to DBRef objects with the MongoDBRef Class and supplies the FetchDBRef Method for accessing these objects.
Java
The DBRef class provides supports for DBRefs from Java.
JavaScript
The mongo shell’s JavaScript interface provides a DBRef.
Perl
The Perl driver contains no support for DBRefs. You can transverse references manually or use the MongoDBx::AutoDeref CPAN module.
PHP
The PHP driver does support DBRefs, including the optional $db reference, through The MongoDBRef class.
Python
The Python driver provides the DBRef class, and the dereference method for interacting with DBRefs.
Ruby
The Ruby Driver supports DBRefs using the DBRef class and the deference method.

Use

In most cases you should use the manual reference method for connecting two or more related documents. However, if you need to reference documents from multiple collections, consider a DBRef.

←   ObjectId GridFS  →