Skip to main content
InterviewHubPRO
Section Overview
Q38

Question 38 of 50

Part 3: Advanced Internals, Scale & Production Patterns (Senior / Lead)

76%
MongoDB & NoSQL (50 Qs)•Part 3: Advanced Internals, Scale & Production Patterns (Senior / Lead)

Advanced Aggregation Pipeline: `$lookup`, `$unwind`, and `$facet`.

senior LevelCode ExamplePipesCI/CD & DevOps

In Simple Words: MongoDB Aggregation pipelines process documents through multi-stage data transforms:

  • $lookup: Performs a left outer join to another collection.
  • $unwind: Deconstructs an array field from input documents to output a document for each element.
  • $facet: Executes multiple parallel aggregation pipelines within a single stage (e.g. calculating price distribution buckets AND total item count in one query).
javascript
db.orders.aggregate([
  { $match: { status: 'completed' } },
  { $lookup: { from: 'users', localField: 'userId', foreignField: '_id', as: 'customer' } },
  { $unwind: '$customer' },
  { $group: { _id: '$customer.country', totalRevenue: { $sum: '$totalAmount' } } },
  { $sort: { totalRevenue: -1 } }
]);