Embedding and Referencing


In MongoDB, you can store related data either by embedding or referencing it.
Embedding means you can store the subdocuments directly within the main document. This is useful when the embedded data is relatively small and not frequently accessed. This approach removes the need for additional queries and reduces the complexity of the data model. Here's how you can embed related data in a MongoDB document using the example of a blog post and its comments in JavaScript:
{
_id: ObjectId("60a63213bd2b0f197c3f3d1d"),
title: "My First Blog Post",
content: "This is my first blog post on Hashnode.",
comments: [
{ author: "user1", text: "Great post." },
{ author: "user2", text: "Thanks for sharing." }
]
}
Referencing, on the other hand, means storing a reference to a related document rather than embedding the entire document. This is useful when the related data is complex or frequently accessed, as it allows for greater flexibility in querying and updating the data. Here's an example of referencing related data in MongoDB using the same blog post and comment entities as above:
// Blog Post Document
{
_id: ObjectId("60a63213bd2b0f197c3f3d1d"),
title: "My First Blog Post",
content: "This is my first blog post on Hashnode.",
comments: [
ObjectId("60a63213bd2b0f197c3f3d1e"),
ObjectId("60a63213bd2b0f197c3f3d1f")
]
}
// Comment Document
{
_id: ObjectId("60a63213bd2b0f197c3f3d1e"),
author: "user1",
text: "Great post."
}
{
_id: ObjectId("60a63213bd2b0f197c3f3d1f"),
author: "user2",
text: "Thanks for sharing."
}
In this example, the blog post document includes an array of comment IDs, which can be used to retrieve the corresponding comment documents using a separate MongoDB query.
Subscribe to my newsletter
Read articles from Preeti samuel directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
Preeti samuel
Preeti samuel
I am Kamilla Preeti Samuel, a Fullstack Developer with a strong command of JavaScript, Node.js, MongoDB, MySQL, CSS, and HTML. Over the years, I have built and worked on a range of applications, gaining valuable hands-on experience in both backend and frontend development. My professional journey includes working as a Junior Software Engineer at Bytestrum, where I focused on software development, and at NUK9 as a UX and UI Designer, contributing to creating user-centered design solutions. I thrive on building efficient, scalable, and user-friendly applications, combining technical expertise with a keen eye for design. I enjoy collaborating with cross-functional teams to create seamless digital experiences, and I am passionate about continuously exploring new tools and frameworks to stay ahead in the fast-evolving tech landscape. I am Kamilla Preeti Samuel, a full-stack developer with a strong command of JavaScript, Node.js, MongoDB, MySQL, CSS, and HTML. Over the years, I have built and worked on various applications, gaining valuable hands-on experience in both backend and frontend development. My professional journey includes working as a Junior Software Engineer at Bytestrum, where I focused on software development, and at NUK9 as a UX and UI Designer, contributing to creating user-centered design solutions. I thrive on building efficient, scalable, and user-friendly applications, combining technical expertise with a keen eye for design. I enjoy collaborating with cross-functional teams to create seamless digital experiences, and I am passionate about continuously exploring new tools and frameworks to stay ahead in the fast-evolving tech landscape.