You are previewing MongoDB in Action.

MongoDB in Action

Cover of MongoDB in Action by Kyle Banker Published by Manning Publications
O'Reilly logo

Appendix B. Design patterns

B.1. Patterns

The early chapters of this book implicitly advocate a certain set of design patterns. Here I’ll summarize those patterns and augment them with a few patterns that fall outside the flow of the text.

B.1.1. Embed versus reference

Suppose you’re building a simple application in MongoDB that stores blog posts and comments. How do you represent this data? Do you embed the comments in their respective blog post documents? Or is it better to create two collections, one for posts and the other for comments, and then relate the comments to the posts with an object id reference?

This is the problem of embedding versus referencing, and it’s a common source of confusion for new users of MongoDB. Fortunately, ...

The best content for your career. Discover unlimited learning on demand for around $1/day.