## 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 name, 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 documents resemble the following document: 

{ "$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. 

## Driver Support for DBRefs 

C The C driver contains no support for DBRefs. You can traverse references manually. 

C++ The C++ driver contains no support for DBRefs. You can traverse references manually. 

C# The C# driver supports DBRefs using the MongoDBRef 17 class and the FetchDBRef 18 method. 

