= 1024) leftSidebarOpen = false; if(window.innerWidth >= 1280) rightSidebarOpen = false"<br>:class="{ 'dark': $store.theme.dark, 'left-sidebar-closed': !leftSidebarDesktop, 'right-sidebar-closed': !rightSidebarDesktop, 'overflow-hidden': leftSidebarOpen || rightSidebarOpen, 'sidebars-closed': !leftSidebarDesktop && !rightSidebarDesktop }">
MongoDB Schema Design: Embedded vs Referenced with Examples
Skip to content
Navigation
if (window.innerWidth
Preferences
Layout
Save Sidebar view
Remember if sidebars are open or closed across pages.
Save Sidebar layout preference
Enable Bookmarks
Save your favourite articles locally in this browser.
Enable Local Bookmarks
let scroll = window.scrollY;<br>let docHeight = document.documentElement.scrollHeight - window.innerHeight;<br>this.progress = docHeight > 0 ? Math.max(0, Math.min(1, scroll / docHeight)) : 0;<br>this.ticking = false;<br>});<br>}"<br>@scroll.window.passive="update()"<br>@resize.window.passive="update()"<br>x-init="update()"<br>class="fixed top-0 left-0 w-full h-[3px] z-[100] pointer-events-none">
MongoDB schema design example in Visualeaf, comparing embedded data inside a users document with referenced data between users and payments.
When you design a MongoDB database, one of the first choices is this:<br>Should this data stay inside the same document, or should it live in another collection?<br>That is the difference between embedding and referencing .<br>Embedding means the data is stored inside the document.<br>Referencing means the data is stored in another collection and connected with an _id.<br>That’s the main difference.<br>Let’s use a simple database called streaming_platform_db.<br>It has collections like users, movies, series, episodes, payments, ratings, watch_history, and activity_logs.<br>The database works like a basic streaming platform.<br>A user can have a subscription, profiles, and devices. The same user can also make payments, watch content, rate movies, and create activity logs.<br>That makes this database a good example because it uses both embedded data and referenced data.<br>Visual Schema Design<br>In Visualeaf, I generated a schema diagram for the streaming_platform_db database.<br>This makes the structure easier to understand because I can see the collections and their relationships in one place.<br>VisuaLeaf schema diagram for streaming_platform_db, showing embedded fields and referenced collections.For example, the users collection has fields like:<br>subscription<br>profiles<br>devicesThese fields are stored inside each user document.<br>That means they are embedded .<br>But other collections, like payments, ratings, watch_history, and activity_logs, are connected to users with user_id.<br>That means they are referenced .<br>So the diagram shows the main idea very clearly:<br>Some data lives inside a document.<br>Some data lives in another collection and points back with an ID.<br>A Quick Example in Mongo Shell<br>To make this easier to see, here is a small example from the users collection.<br>In this document, subscription, profiles, and devices are stored inside the user document.<br>That means they are embedded .<br>db.users.insertOne({<br>full_name: "Oliver Smith",<br>email: "oliver.smith@streaming.test",<br>country: "UK",<br>city: "London",
subscription: {<br>plan: "standard",<br>status: "active"<br>},
profiles: [<br>profile_name: "Oliver",<br>type: "adult",<br>preferences: {<br>favorite_genres: ["Action", "Sci-Fi"],<br>subtitles: ["en"]<br>},<br>profile_name: "Kids",<br>type: "kids",<br>kids_mode: true<br>],
devices: [<br>type: "laptop",<br>os: "Windows 11"<br>});After creating this document in Mongo Shell, Visualeaf makes the embedded structure easier to see. The fields are inside the users document, not in separate collections.<br>Embedded data in the users collection. The subscription, profiles, preferences, and devices are stored inside the same user document.Now compare that with referenced data.<br>Payments are not stored inside the user document. They live in a separate collection and point back to the user with user_id.<br>db.payments.insertOne({<br>user_id: ObjectId("USER_ID_HERE"),<br>amount: 12.99,<br>currency: "EUR",<br>status: "paid",<br>paid_at: new Date()<br>});Here, user_id connects the payment to the user.<br>That is a reference .<br>Visualeaf schema view showing the reference between users and payments through user_id.One important thing to know: a MongoDB reference is not the same as a SQL foreign key.<br>In SQL, a foreign key can enforce the relationship between two tables.<br>In MongoDB, user_id is just a field that stores another document’s _id.<br>MongoDB does not automatically check that the user exists.<br>So the difference from the main diagram is simple:<br>users.devices // embedded<br>users.profiles // embedded<br>users.subscription // embedded
payments.user_id // referenced<br>ratings.user_id // referenced<br>watch_history.user_id // referenced<br>activity_logs.user_id // referencedEmbed data when it belongs inside the document.<br>Reference data when it should stay in another collection.<br>Where embedding fits<br>Embedding is a good choice when your data is associated with one primary...