0
0
MongoDBquery~10 mins

$count accumulator 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 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'
A$count
B$sum
C$avg
D$max
Attempts:
3 left
💡 Hint
Common Mistakes
Using $sum instead of $count to count documents.
Using $avg or $max which do not count documents.
2fill in blank
medium

Complete 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'
A$count
B$sum
C$min
D$max
Attempts:
3 left
💡 Hint
Common Mistakes
Using $sum: 1 instead of $count.
Using $min or $max which do not count documents.
3fill in blank
hard

Fix 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'
A$avg
B$sum
C$count
D$max
Attempts:
3 left
💡 Hint
Common Mistakes
Using $sum: 1 instead of $count: {}.
Passing a value to $count which expects an empty object.
4fill in blank
hard

Fill 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'
A$count
B$gt
C$gte
D$sum
Attempts:
3 left
💡 Hint
Common Mistakes
Using $sum instead of $count in $group.
Using $gte instead of $gt for filtering.
5fill in blank
hard

Fill 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'
A"$category"
B$count
C$gt
D$sum
Attempts:
3 left
💡 Hint
Common Mistakes
Using $sum instead of $count.
Using $gte instead of $gt.
Not quoting the field name in _id.