Hey folks! I'm wondering how you typically model M...
# mongodb
m
Hey folks! I'm wondering how you typically model M:N relationships in MongoDB: • Do you embed an array of IDs inside the collection that point to other collections? This would be a de-normalized approach. e.g.
Users[0].posts = [post_id1, post_id2, post_id3]
• Use a "join collection" that references two related collections. This would be a normalized approach. e.g.
UserPosts = [user_id1, post_id2]
Or do you use both depending on the situation?
a
both depending on the situation and amount of relationships
scale / performance considerations basically
e.g. user relations makes sense in a join collection, referencing specific entries on an object that won’t be more than maybe 50 is fine on an object directly
you can easily have objects with thousands of references in an object btw
maybe not the best approach, but definitely works
m
Thanks for your response @Aske! That was also my intuition that we need to support both ways.
💯 1
👍 1