Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to count all documents in the collection.
MongoDB
db.collection.aggregate([{ $group: { _id: null, total: { [1]: {} } } }]) Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using $sum instead of $count to count documents.
Using $avg or $max which do not count documents.
✗ Incorrect
The $count accumulator counts the number of documents in the group.
2fill in blank
mediumComplete the code to count documents grouped by category.
MongoDB
db.collection.aggregate([{ $group: { _id: "$category", count: { [1]: {} } } }]) Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using $sum: 1 instead of $count.
Using $min or $max which do not count documents.
✗ Incorrect
Use $count to count documents per group by category.
3fill in blank
hardFix the error in the aggregation to count documents by status.
MongoDB
db.collection.aggregate([{ $group: { _id: "$status", total: { [1]: 1 } } }]) Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using $sum: 1 instead of $count: {}.
Passing a value to $count which expects an empty object.
✗ Incorrect
The $count accumulator does not take a value; $sum: 1 counts documents but $count is the correct accumulator here.
4fill in blank
hardFill both blanks to count documents grouped by type and filter groups with count greater than 5.
MongoDB
db.collection.aggregate([
{ $group: { _id: "$type", count: { [1]: {} } } },
{ $match: { count: { [2]: 5 } } }
]) Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using $sum instead of $count in $group.
Using $gte instead of $gt for filtering.
✗ Incorrect
Use $count to count documents per type and $gt to filter counts greater than 5.
5fill in blank
hardFill all three blanks to count documents by category, filter counts greater than 10, and sort descending by count.
MongoDB
db.collection.aggregate([
{ $group: { _id: [1], count: { [2]: {} } } },
{ $match: { count: { [3]: 10 } } },
{ $sort: { count: -1 } }
]) Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using $sum instead of $count.
Using $gte instead of $gt.
Not quoting the field name in _id.
✗ Incorrect
Group by "$category", count documents with $count, filter counts greater than 10 with $gt.