Introduction to MongoDB Database References

If You are interested to learn about the MongoDb Relationship

As was seen in the final chapter on MongoDB relationships, we employ the idea of Referenced Relationships, also known as Manual References, in which we manually store the id of the referenced document inside another document in order to construct a normalised database structure in MongoDB. However, we can use MongoDB DBRefs when a document has references from many collections.

Definition of Reference Database

Reference Database refers to the database kept by Bacs that contains information entered about you by Bacs, us, and you, as appropriate; this includes, for example, the levels of authorization and permission pertaining to Bacstel-IP Transmissions that you have submitted to Bacs as part of Bacstel-IP; TERMS & CONDITIONS “Service” refers to the Bacstel-IP service that we provide in line with the terms and conditions of this Agreement and the User Guides, including but not limited to any services related to direct debit. (continued)

DBRefs vs Manual References

Consider a database where different sorts of addresses (home, office, mailing, etc.) are stored in various collections (address home, address office, address mailing, etc.). In this case, we would utilise DBRefs rather than manual references. Now, depending on the type of address referenced, a user collection’s document must also specify which collection should be used to seek up the address. We should use DBRefs in situations like these, where a document references documents from numerous collections.

Using DBRefs

There are three fields in DBRefs −

  • $ref − This field specifies the collection of the referenced document
  • $id − This field specifies the _id field of the referenced document
  • $db − This is an optional field and contains the name of the database in which the referenced document lies

Consider a sample user document having DBRef field address as shown in the code snippet −

{
   "_id":ObjectId("53402597d852426020000002"),
   "address": {
   "$ref": "address_home",
   "$id": ObjectId("534009e4d852427820000002"),
   "$db": "tutorialspoint"},
   "contact": "987654321",
   "dob": "01-01-1991",
   "name": "Tom Benzamin"
}

The address home collection is where the referred address document is located, according to the address DBRef field.

The following code dynamically searches for a document with the id supplied by the $id argument in DBRef in the collection specified by the $ref parameter (in our example, address home).

>var user = db.users.findOne({"name":"Tom Benzamin"})
>var dbRef = user.address
>db[dbRef.$ref].findOne({"_id":(dbRef.$id)})

The above code returns the following address document present in address_home collection −

{
   "_id" : ObjectId("534009e4d852427820000002"),
   "building" : "22 A, Indiana Apt",
   "pincode" : 123456,
   "city" : "Los Angeles",
   "state" : "California"
}

Driver Support for DBRefs

DriverDBRef SupportNotes
CNot SupportedYou can traverse references manually.
C++Not SupportedYou can traverse references manually.
C#SupportedPlease see the C# driver page for more information.
GoNot SupportedYou can traverse references manually.
HaskellNot SupportedYou can traverse references manually.
JavaSupportedPlease see the Java driver page for more information.
Node.jsSupportedPlease see the Node.js driver page for more information.
PerlSupportedPlease see the Perl driver page for more information.
PHPNot SupportedYou can traverse references manually.
PythonSupportedPlease see the PyMongo driver page for more information.
RubySupportedPlease see the Ruby driver page for more information.
ScalaNot SupportedYou can traverse references manually.

Examples of Reference Database in a sentence

The rolling stock data must be kept in a rolling stock reference database, which belongs to the keeper of the rolling stock.

The System Reference Database Files must have this template in their storage. Any changes to these contact details that are stored in the Reference Database must be immediately reported to the Bank.

The following categories apply to the entries in the Rolling Stock Reference Database: — Administrative information pertaining to certification and registration items, such as the notified body’s identification number and a reference to the EC registration file;

past ownership, leasing, and other data might be included. the Draize eye test Reference Database, a compilation of historical serious eye damage/eye irritation in vivo data examined by classification drivers to enable chemical choice for development and evaluation of substitute methods/strategies (DRD).

Introduction to MongoDB Database References
Show Buttons
Hide Buttons