0
0
MongoDBquery~10 mins

How the engine optimizes pipelines in MongoDB - Interactive Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to start an aggregation pipeline with the correct stage operator.

MongoDB
db.collection.aggregate([[1]])
Drag options to blanks, or click blank then click option'
A{ $match: { status: 'A' } }
B{ $filter: { input: '$items', as: 'item', cond: { $gt: ['$$item.qty', 10] } } }
C{ $group: { _id: '$cust_id', total: { $sum: '$amount' } } }
D{ $sort: { date: 1 } }
Attempts:
3 left
💡 Hint
Common Mistakes
Using a stage that manipulates arrays as the first stage.
Starting with a grouping stage without filtering first.
2fill in blank
medium

Complete the code to optimize the pipeline by placing the correct stage early.

MongoDB
db.collection.aggregate([[1], { $group: { _id: '$category', total: { $sum: '$price' } } }])
Drag options to blanks, or click blank then click option'
A{ $sort: { price: -1 } }
B{ $project: { price: 1, category: 1 } }
C{ $match: { price: { $gt: 100 } } }
D{ $limit: 5 }
Attempts:
3 left
💡 Hint
Common Mistakes
Using $project before filtering, which does not reduce documents.
Sorting before filtering, which can be inefficient.
3fill in blank
hard

Fix the error in the pipeline by choosing the correct stage operator.

MongoDB
db.collection.aggregate([{ $match: { status: 'A' } }, [1]])
Drag options to blanks, or click blank then click option'
A{ $group: { _id: '$cust_id', total: { $sum: '$amount' } } }
B{ $sortByCount: '$category' }
C{ $filter: { input: '$items', as: 'item', cond: { $gt: ['$$item.qty', 10] } } }
D{ $unwind: '$items' }
Attempts:
3 left
💡 Hint
Common Mistakes
Using $filter as a pipeline stage, which is an expression, not a stage.
Using $unwind incorrectly without context.
4fill in blank
hard

Fill both blanks to optimize the pipeline by filtering and projecting fields.

MongoDB
db.collection.aggregate([[1], [2]])
Drag options to blanks, or click blank then click option'
A{ $match: { status: 'active' } }
B{ $project: { name: 1, status: 1, _id: 0 } }
C{ $group: { _id: '$status', count: { $sum: 1 } } }
D{ $sort: { name: 1 } }
Attempts:
3 left
💡 Hint
Common Mistakes
Projecting fields before filtering, which processes unnecessary data.
Grouping before filtering, which can be inefficient.
5fill in blank
hard

Fill all three blanks to build an optimized pipeline with filtering, unwinding, and grouping.

MongoDB
db.collection.aggregate([[1], [2], [3]])
Drag options to blanks, or click blank then click option'
A{ $match: { tags: 'mongodb' } }
B{ $unwind: '$tags' }
C{ $group: { _id: '$tags', count: { $sum: 1 } } }
D{ $sort: { count: -1 } }
Attempts:
3 left
💡 Hint
Common Mistakes
Unwinding before filtering, which processes more data than needed.
Grouping before unwinding, which groups arrays instead of elements.