Lesson 39/100

Tutorials MongoDB Tutorial

Outlier Pattern

Outlier Pattern: free step-by-step lesson with examples, common mistakes, and interview tips — part of MongoDB Tutorial on Toolliyo Academy.

On this page

MongoDB Tutorial · Lesson 39 of 100

Outlier Pattern

Foundations & CRUD ✓Queries & SchemaAggregation & ScaleAtlas & Projects

Queries & Schema · 2 — Design · ~6 min · MongoDB — Schema Design

What is this?

The outlier pattern keeps normal documents small and moves rare huge cases to a separate structure — for example most users have <50 friends embedded, but celebrities reference an overflow collection.

Why should you care?

One viral post with a million likes should not force every post document to use the heavy design. Handle the exception separately.

See it live — copy this example

Open mongosh or MongoDB Compass, select database nosqlverse, then run the example. Change one field and run again.

db.posts.insertOne({
  _id: ObjectId("64b000000000000000000001"),
  title: "Normal post",
  likeCount: 12,
  likedBy: [1, 2, 3] // small list embedded
})
db.posts.insertOne({
  _id: ObjectId("64b000000000000000000099"),
  title: "Viral post",
  likeCount: 1000000,
  likesOverflow: true
})
db.postLikes.insertOne({
  postId: ObjectId("64b000000000000000000099"),
  userId: 42,
  at: new Date()
})
db.posts.find({ likesOverflow: { $ne: true } })

Run Example »

Edit the code below and click Run to see the result in Toolliyo’s live editor.

Code
Result

What happened?

  • Normal posts embed a small likedBy array.
  • The viral post sets likesOverflow and stores individual likes in postLikes.
  • Most reads stay simple; outliers use the side collection.

Practice next

  1. Insert both posts and one overflow like.
  2. Write app logic: if likesOverflow then query postLikes.
  3. Convert a post to overflow when likedBy.length exceeds 100.
  4. Add an overflow threshold constant in your notes (e.g. 100).
  5. Index postLikes on { postId: 1, userId: 1 } uniquely.

Remember

Optimize for the common case. Move rare huge relations aside. Flag outliers explicitly.

Celebrity account followers

A social app embeds follower ids for normal users but stores celebrity followers in a sharded followers collection.

Outcome: 99% of profiles stay tiny; mega-accounts still scale.

Interview prep for this lesson

Practice these questions aloud after reading—each links to a full structured answer.

Junior Detailed
Explain SQL queries in the context of MongoDB.
Short answer: Interviewers want a crisp definition, a practical example from your projects, and awareness of trade-offs—not textbook dumps. Explain a bit more How to structure your answer (60–90 seconds) Define SQL queri…
Mid Detailed
What are common mistakes teams make with Schema design when using MongoDB?
Short answer: Interviewers want a crisp definition, a practical example from your projects, and awareness of trade-offs—not textbook dumps. Explain a bit more How to structure your answer (60–90 seconds) Define Schema de…
Senior Detailed
How would you debug a production issue related to Transactions in a MongoDB application?
Short answer: Interviewers want a crisp definition, a practical example from your projects, and awareness of trade-offs—not textbook dumps. Explain a bit more How to structure your answer (60–90 seconds) Define Transacti…
Junior Detailed
Describe a real-world scenario where Normalization mattered in a MongoDB project.
Short answer: Interviewers want a crisp definition, a practical example from your projects, and awareness of trade-offs—not textbook dumps. Explain a bit more How to structure your answer (60–90 seconds) Define Normaliza…
Questions on this lesson 0

Sign in to ask a question or upvote helpful answers.

No questions yet — be the first to ask!

MongoDB Tutorial
Course syllabus

MongoDB Tutorial

MongoDB — Foundations
MongoDB — CRUD Operations
MongoDB — Query Operators
MongoDB — Schema Design
MongoDB — Indexing & Performance
MongoDB — Aggregation Pipelines
MongoDB — Replication & Sharding
MongoDB — Atlas & Security
MongoDB — Modern Features
MongoDB — Real-World Projects
Toolliyo Assistant
Ask about tutorials, ebooks, training, pricing, mentor services, and support. I use public site content only—not admin or internal tools.

care@toolliyo.com

Need callback? Share your details