0
0
MongoDBquery~10 mins

Pipeline execution order matters 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 a match stage filtering documents where age is greater than 25.

MongoDB
db.users.aggregate([ { $[1]: { age: { $gt: 25 } } } ])
Drag options to blanks, or click blank then click option'
Amatch
Bgroup
Csort
Dproject
Attempts:
3 left
💡 Hint
Common Mistakes
Using $group instead of $match will cause an error because grouping requires a different structure.
Using $sort or $project here won't filter documents.
2fill in blank
medium

Complete the code to sort the documents by score in descending order after filtering.

MongoDB
db.users.aggregate([ { $match: { active: true } }, { $[1]: { score: -1 } } ])
Drag options to blanks, or click blank then click option'
Aproject
Bgroup
Csort
Dlimit
Attempts:
3 left
💡 Hint
Common Mistakes
Using $group instead of $sort will aggregate documents, not order them.
Using $limit will restrict the number of documents but not sort them.
3fill in blank
hard

Fix the error in the pipeline by placing the $project stage correctly to include only name and score fields.

MongoDB
db.users.aggregate([ { $[1]: { score: { $gt: 50 } } }, { $project: { name: 1, score: 1 } } ])
Drag options to blanks, or click blank then click option'
Agroup
Bmatch
Csort
Dlimit
Attempts:
3 left
💡 Hint
Common Mistakes
Placing $match after $project can cause unexpected results or errors.
Using $group here is incorrect because we want to filter, not aggregate.
4fill in blank
hard

Fill both blanks to correctly filter active users and then group them by city counting the number of users per city.

MongoDB
db.users.aggregate([ { $[1]: { active: true } }, { $[2]: { _id: "$city", count: { $sum: 1 } } } ])
Drag options to blanks, or click blank then click option'
Amatch
Bproject
Cgroup
Dsort
Attempts:
3 left
💡 Hint
Common Mistakes
Using $project instead of $match will not filter documents.
Using $sort instead of $group will not aggregate counts.
5fill in blank
hard

Fill all three blanks to filter users older than 30, sort by age ascending, and project only name and age fields.

MongoDB
db.users.aggregate([ { $[1]: { age: { $gt: 30 } } }, { $[2]: { age: 1 } }, { $[3]: { name: 1, age: 1, _id: 0 } } ])
Drag options to blanks, or click blank then click option'
Amatch
Bsort
Cproject
Dgroup
Attempts:
3 left
💡 Hint
Common Mistakes
Changing the order of stages can lead to wrong results.
Using $group instead of $project will aggregate instead of selecting fields.