0
0
MongoDBquery~10 mins

Pipeline mental model (stages flow) in MongoDB - Interactive Code 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 first stage.

MongoDB
db.collection.aggregate([ { [1]: { age: { $gt: 25 } } } ])
Drag options to blanks, or click blank then click option'
A$match
B$group
C$sort
D$project
Attempts:
3 left
💡 Hint
Common Mistakes
Using $group as the first stage without filtering.
Using $sort before filtering documents.
2fill in blank
medium

Complete the code to add a stage that groups documents by 'department' and counts them.

MongoDB
db.collection.aggregate([ { $match: { status: 'active' } }, { [1]: { _id: "$department", count: { $sum: 1 } } } ])
Drag options to blanks, or click blank then click option'
A$project
B$group
C$sort
D$limit
Attempts:
3 left
💡 Hint
Common Mistakes
Using $project instead of $group for grouping.
Trying to count documents in $match stage.
3fill in blank
hard

Fix the error in the pipeline stage that sorts documents by 'score' descending.

MongoDB
db.collection.aggregate([ { $match: { active: true } }, { [1]: { score: -1 } } ])
Drag options to blanks, or click blank then click option'
A$sort
B$limit
C$match
D$group
Attempts:
3 left
💡 Hint
Common Mistakes
Using $group or $match instead of $sort for ordering.
Using positive 1 for descending order.
4fill in blank
hard

Fill both blanks to project only the 'name' and 'total' fields in the output.

MongoDB
db.collection.aggregate([ { $match: { status: 'complete' } }, { [1]: [2] } ])
Drag options to blanks, or click blank then click option'
A$project
B{ name: 1, total: 1 }
C{ name: 0, total: 0 }
D$group
Attempts:
3 left
💡 Hint
Common Mistakes
Using $group instead of $project to select fields.
Setting fields to 0 to include them.
5fill in blank
hard

Fill all three blanks to create a pipeline that filters active users, groups by city counting users, and sorts by count descending.

MongoDB
db.users.aggregate([ { [1]: { active: true } }, { [2]: { _id: "$city", userCount: { $sum: 1 } } }, { [3]: { userCount: -1 } } ])
Drag options to blanks, or click blank then click option'
A$match
B$group
C$sort
D$project
Attempts:
3 left
💡 Hint
Common Mistakes
Mixing the order of stages.
Using $project instead of $group for counting.