Choosing by Relationship: One-to-Few, One-to-Many, Many-to-Many
How many documents sit on each side of a relationship points to its model; embed a few, reference from the many side, never keep an array that grows without end, and link many-to-many with an array of ids.
Journeyman · MongoDB · NoSQL · Updated
After this lesson you can
- Sort a relationship into one-to-few, one-to-many, one-to-squillions or many-to-many, and pick the model each usually gets
- Spot an array that never stops growing, and move its items into their own collection with a reference back
- Choose embedding, a reference or an array of ids for a store's relationship, from how the app reads and changes the data
Key takeaways
- One-to-few, such as a bike's photos, is usually embedded, because the list stays small and is read with its owner.
- One-to-many and one-to-squillions, such as a customer's orders or a product's page views, keep the reference on the many side.
- Many-to-many, such as products and categories, keeps an array of ids on the side that has fewer links per document.
- An array that grows without end is the mistake to avoid. Move its items into their own collection, each pointing back.
Every app has relationships: a user and their addresses, a playlist and its songs, a course and its students. Each time you add one, you'll decide whether to embed it or link it, and the number of things on each side is the first clue. Get it right, and the documents stay small and fast for years.
The examples use Riverbend's store from Model for the Questions Your App Asks. If you changed it in an earlier lesson, load("riverbend-store.js") puts it back as it started.
Three relationships, three answers
Riverbend's store already answers the same kind of question three different ways. A product's photos are inside the product. A customer's orders are not inside the customer; each order points at her. And a product's categories are a list of ids. A rule that said "always embed" or "always reference" would get two of the three wrong.
What differs is how many things sit on each side of the link, and whether that number stays small. A relationship is described by those numbers, read aloud as "one-to-few", "one-to-many" and so on.
One-to-few: embed
A bike has a few photos, and the product page shows them with the bike. That's one-to-few: one owner, a handful of things that won't grow past a known limit. Embed them:
db.products.findOne({ _id: 102 }, { name: 1, photos: 1 })
{
_id: 102,
name: 'Canal City Bike',
photos: [ 'canal-side.jpg', 'canal-basket.jpg' ]
}
A customer's address, or three saved addresses, is one-to-few too. One read gets the owner and its list.
One-to-many and one-to-squillions: reference from the many side
Alex has two orders today. Next year she may have twenty, and a business customer could have thousands. That's one-to-many. If her customer document held an array of her orders, it would grow with every purchase. So the link goes the other way: each order holds customerId, and the customer document never changes when she buys something.
db.orders.countDocuments({ customerId: 1 })
2
Some lists have no end at all. Every time anyone opens the Canal City Bike's page, the shop may want to record it: thousands of views a day, forever. People who design databases jokingly call this one-to-squillions, more than anyone could count. An array of views inside the product, views: [ ... ], would be an unbounded array: one that never stops growing. Every read of the product would drag every view along, and one day the product would pass the 16 MB document limit and refuse to save. Instead, each view is its own small document in a pageViews collection, holding productId: 102. The product stays the same size however popular it gets.
Many-to-many: an array of ids
A product can be in several categories, and a category holds many products: many-to-many. Embedding copies of whole categories into products, or of products into categories, would mean keeping copies up to date on both sides. Instead, each product holds a short array of category ids, categoryIds. The category page asks which products list it, the way Querying Arrays: One Value, $all and $size showed:
db.products.find({ categoryIds: "lights" }, { name: 1 })
[
{ _id: 105, name: 'Beacon Front Light' },
{ _id: 106, name: 'Beacon Rear Light' }
]
And the product page finds its categories' names with $in, reading "in", on the list from the product:
db.categories.find({ _id: { $in: ["city-bikes", "commuting"] } })
[
{ _id: 'city-bikes', name: 'City Bikes' },
{ _id: 'commuting', name: 'Commuting' }
]
Which side holds the array? The side with fewer links per document. A product is in two or three categories; a category may hold hundreds of products. So the array lives in the product, where it stays short.
The rules of thumb, side by side
| Relationship | Riverbend's example | How many on the many side | Usual model |
|---|---|---|---|
| One-to-few | A product and its photos | A handful, with a limit | Embed in the product |
| One-to-many | A customer and her orders | Dozens to thousands, growing | Each order holds customerId |
| One-to-squillions | A product and its page views | Millions, never stopping | Each view holds productId |
| Many-to-many | Products and categories | Several on each side | An array of ids in each product |
They're rules of thumb, not laws. The screens and questions from Model for the Questions Your App Asks still decide: a few items that change all the time, or that another screen reads on their own, may be better referenced even though there are only a few. Whatever you choose, ask one more question of every array: does it have a limit? If not, give its items a collection of their own.

Check yourself
2 questions to try on the lesson page.
So what
Count what's on each side, ask whether it has a limit, and the model follows: embed the few, reference from the many, link many-to-many with a short array of ids, and never let an array grow without end.