In MongoDB, references are used to establish relationships between documents in different collections. This is similar to foreign keys in SQL databases. References allow you to normalize your data by storing related information in separate documents and linking them together.

Key Concepts

  1. Normalization: Storing related data in separate collections and linking them using references.
  2. Manual References: Storing the _id of one document in another document.
  3. DBRefs: A convention for referencing documents in MongoDB, which includes the collection name and the document's _id.

Manual References

Manual references involve storing the _id of one document inside another document. This is the most common way to create references in MongoDB.

Example

Consider two collections: authors and books.

authors Collection:

{
  "_id": ObjectId("60c72b2f9b1d8b3f4c8b4567"),
  "name": "J.K. Rowling"
}

books Collection:

{
  "_id": ObjectId("60c72b3f9b1d8b3f4c8b4568"),
  "title": "Harry Potter and the Philosopher's Stone",
  "author_id": ObjectId("60c72b2f9b1d8b3f4c8b4567")
}

In this example, the books collection has a field author_id that stores the _id of the corresponding author in the authors collection.

Querying with Manual References

To find the author of a specific book, you can perform a query using the author_id.

Find the author of a specific book:

const book = db.books.findOne({ title: "Harry Potter and the Philosopher's Stone" });
const author = db.authors.findOne({ _id: book.author_id });

DBRefs

DBRefs are a convention for referencing documents in MongoDB. They include three fields: $ref, $id, and $db.

  • $ref: The name of the collection.
  • $id: The _id of the referenced document.
  • $db: The name of the database (optional).

Example

authors Collection:

{
  "_id": ObjectId("60c72b2f9b1d8b3f4c8b4567"),
  "name": "J.K. Rowling"
}

books Collection:

{
  "_id": ObjectId("60c72b3f9b1d8b3f4c8b4568"),
  "title": "Harry Potter and the Philosopher's Stone",
  "author": {
    "$ref": "authors",
    "$id": ObjectId("60c72b2f9b1d8b3f4c8b4567")
  }
}

Querying with DBRefs

To resolve a DBRef, you need to perform a manual lookup.

Find the author of a specific book:

const book = db.books.findOne({ title: "Harry Potter and the Philosopher's Stone" });
const author = db[book.author.$ref].findOne({ _id: book.author.$id });

Practical Exercise

Exercise

  1. Create two collections: students and courses.
  2. Insert documents into both collections.
  3. Create a reference from the students collection to the courses collection.
  4. Write a query to find the course details for a specific student.

Solution

Step 1: Create Collections

db.createCollection("students");
db.createCollection("courses");

Step 2: Insert Documents

db.courses.insertOne({
  "_id": ObjectId("60c72b4f9b1d8b3f4c8b4569"),
  "course_name": "Introduction to MongoDB"
});

db.students.insertOne({
  "_id": ObjectId("60c72b5f9b1d8b3f4c8b4570"),
  "name": "John Doe",
  "course_id": ObjectId("60c72b4f9b1d8b3f4c8b4569")
});

Step 3: Query to Find Course Details

const student = db.students.findOne({ name: "John Doe" });
const course = db.courses.findOne({ _id: student.course_id });

Summary

  • Manual References: Store the _id of one document in another document to create a reference.
  • DBRefs: A convention for referencing documents that includes the collection name and the document's _id.
  • Querying: Use the stored _id to perform lookups and retrieve related documents.

Understanding references in MongoDB is crucial for designing normalized data models and efficiently managing relationships between documents. In the next section, we will explore different data types supported by MongoDB.

© Copyright 2024. All rights reserved