What is Embedding in MongoDB: Explanation and Examples
embedding means storing related data inside a single document as nested objects or arrays. This keeps related information together, making data retrieval faster and simpler without needing joins.How It Works
Embedding in MongoDB works by placing related data inside a single document rather than in separate tables or collections. Imagine a folder where you keep all papers related to a project together instead of scattering them in different drawers. This way, when you open the folder, you get everything you need at once.
For example, if you have a user and their addresses, embedding means storing the addresses inside the user's document as a list. This avoids looking up addresses separately and speeds up reading the user's full information.
Embedding is great when the related data is closely connected and usually accessed together. It reduces the need for complex queries and improves performance for many common tasks.
Example
This example shows a MongoDB document where a user's addresses are embedded inside the user document as an array.
db.users.insertOne({
name: "Alice",
email: "alice@example.com",
addresses: [
{ street: "123 Maple St", city: "Springfield", zip: "12345" },
{ street: "456 Oak St", city: "Greenville", zip: "67890" }
]
})
// Query to find user and their addresses
const user = db.users.findOne({ name: "Alice" })
printjson(user)When to Use
Use embedding when related data is mostly read or written together. For example, storing a blog post with its comments embedded if comments are few and usually accessed with the post.
Embedding is ideal for one-to-few relationships where the nested data won't grow too large. It simplifies queries and improves speed by avoiding separate lookups.
However, if the related data is large or changes independently, referencing separate documents might be better to keep data manageable.
Key Points
- Embedding stores related data inside a single MongoDB document.
- It improves read performance by keeping data together.
- Best for closely related data accessed together.
- Not suitable for large or frequently changing nested data.