MongoDB Schema Design Best Practices

Joe Carlson · 184K views · 2020 · 50:39

Open on YouTube

Databases & storageSoftware architecture & designtalk

Why Stack Overflow recommends it

Stack Overflow users link it when someone is unsure how to structure MongoDB or Mongoose relationships, such as embedding versus referencing, or parent-to-child versus child-to-parent links.

Recommended by 3 people across 3 different months, 2020 to 2022.

What it is

A 50-minute livestream talk by Joe Karlsson, a MongoDB developer advocate, on how to model data in MongoDB. It covers embedding versus referencing, one-to-many and many-to-many relationships, and how read/write patterns and data growth should drive schema choices, using real-world examples.

What people said

Strong rec raphael.oester · 2022-05 · Stack Overflow · on “Which relation in MongoDB is better?”

It depends on the amount of entries there will be in the vehicles array. If you plan to store less than a couple of hundreds vehicles, embedding dependancies in an array is the best solution because it allows to retrieve all your user data at once. That means you don't even have to store only the vehicle's id in the array, you can just put the whole object. This part depends on the way your application has to access the data: If you don't ever use the vehicles independantly of the users, then embedding the whole vehicle documents is good because you can retrieve all your data at once without joining two different collections (expensive). Although if you do need those collections independantl

Recommends Tony Drummond · 2020-10 · Stack Overflow · on “how to add more fields(in mongoose schema) into a field(which is an array of objects) and these objects are reference to another mongoose schema?”

Please see my revision/edit above. I really think you should spend some time learning about how to build models correctly and the design of your database. Here's a great video by a guy who works for MongoDB that talks about database design: [this video]

Recommends Mertafor · 2022-08 · Stack Overflow · on “MongoDB child to parent or parent to child referencing advantages”

I'll answer my own question in case it helps someone. I was looking for use cases of parent to child referencing and found the answer in this video : [this video] Example case especially useful for Many to Many referencing : Users : { _id: 'userId1', name: 'User1', posts: ['postId1', 'postId7'] } { _id: 'userId2', name: 'User2', posts: ['postId1', 'postId4'] } Posts : { _id: 'postId1', title: 'A post title', owners: ['userId1', 'userId2'] } This use case is referencing many to many . Getting a post by userId will be costly because query must lookup in owners field which is an array. Instead storing posts as an array in Users collection and pulling posts by Id would be much practical and faster. O

Cites Omer Setty · 2022-08 · Stack Overflow · on “Mongoose reference in Collection for another Collection”

It is a pity to prescribe the same additives for each products ( each pizza) So, can I create a new model of extraOptions for pizzas and create some 'reference' (like in SQL but in Mongo) for collections Product? Your assumption is correct - there is no need to store the same data of additives in each one of your products. Indeed, the right approach is to extract the additives data to a different collection, and reference the relevant additives for each product. This way, your ProductSchema will look like this (for brevity I excluded the 'required: false' field since false is the default value): import mongoose from "mongoose"; const ProductSchema = mongoose.Schema({ title: { type: String, r