Skip to content
Learn Netverks

Lesson

Step 27/36 75% through track

unwind-bucket-stages

$unwind and $bucket

Last reviewed Jun 1, 2026 Content v20260601
Track mode
none
Means
Read / quiz
Reading
~1 min
Level
intermediate

This lesson

This lesson teaches $unwind and $bucket: document modeling, query operators, and aggregation patterns for MongoDB.

Teams query $unwind and $bucket on every MongoDB codebase—skipping it leaves gaps in debugging and data reviews.

You will apply $unwind and $bucket in contexts like: Content catalogs, event logs, mobile sync backends, and polyglot stacks beside SQL services.

Copy JavaScript shell queries from each lesson into mongosh or MongoDB Atlas Data Explorer—inspect matched documents and explain plans. The in-browser lab (execution_profile: none) ships later; mongosh is the practice path now.

When you can explain the previous lesson's ideas without copying example queries verbatim.

$unwind deconstructs arrays into one doc per element. $bucket histograms numeric values.

Unwind tags

db.products.aggregate([
  { $unwind: '$tags' },
  { $group: { _id: '$tags', count: { $sum: 1 } } },
  { $sort: { count: -1 } }
])

Practice: Run aggregation pipelines in mongosh.

Bucket prices

db.products.aggregate([
  { $bucket: {
      groupBy: '$price',
      boundaries: [0, 10, 25, 50, 100],
      default: '100+',
      output: { count: { $sum: 1 } }
  }}
])

Important interview questions and answers

  1. Q: preserveNullAndEmptyArrays?
    A: Keeps docs when array missing/empty after unwind.
  2. Q: $facet?
    A: Runs parallel sub-pipelines—dashboards in one round trip.

Self-check

  1. Use case for $unwind on tags?
  2. What does $bucket groupBy?

Tip: $unwind tag arrays before counting tag popularity.

Interview prep

$bucket?
Histograms numeric values into ranges.
$facet?
Runs parallel sub-pipelines in one command.

Interview tip Lesson completion confidence

Can you explain this lesson in 30 seconds without reading notes?

Not saved yet.

Check yourself

Multiple choice — immediate feedback.

Discussion

Past discussion is visible to everyone. Only logged-in users can post comments and replies.

Starter discussion topics

  • $facet use?
  • $bucket ranges?

Sign up or log in to post comments and sync lesson progress across devices.

No discussion yet. Be the first to ask a question.

Jump