Challenge - 5 Problems
CountDocuments Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ query_result
intermediate2:00remaining
Counting documents with a simple filter
Given a MongoDB collection named
Assume the collection has 5 documents where
users with documents containing an age field, what is the output of the following code?db.users.countDocuments({ age: { $gt: 30 } })Assume the collection has 5 documents where
age is greater than 30.MongoDB
db.users.countDocuments({ age: { $gt: 30 } })Attempts:
2 left
💡 Hint
The
countDocuments method counts documents matching the filter.✗ Incorrect
The method
countDocuments returns the number of documents that match the filter. Here, it counts documents where age is greater than 30, which is 5.📝 Syntax
intermediate2:00remaining
Identify the syntax error in countDocuments usage
Which option contains a syntax error when using
countDocuments to count documents where status is 'active'?MongoDB
db.users.countDocuments({ status: 'active' })Attempts:
2 left
💡 Hint
Filters use colon
: to assign values, not double equals ==.✗ Incorrect
Option B uses
== inside the filter object, which is invalid syntax in MongoDB queries. Filters require key-value pairs with colon.❓ optimization
advanced2:00remaining
Optimizing countDocuments with an index
You want to count documents where
category is 'books' in a large collection. Which option will make countDocuments run faster?Attempts:
2 left
💡 Hint
Indexes help MongoDB find matching documents faster.
✗ Incorrect
Creating an index on the
category field allows MongoDB to quickly locate documents matching the filter, speeding up countDocuments.🧠 Conceptual
advanced2:00remaining
Difference between countDocuments and estimatedDocumentCount
Which statement correctly describes the difference between
countDocuments and estimatedDocumentCount?Attempts:
2 left
💡 Hint
One method can use filters, the other cannot.
✗ Incorrect
countDocuments counts documents matching a filter and is accurate. estimatedDocumentCount returns a fast approximate count of all documents without filtering.🔧 Debug
expert2:00remaining
Why does countDocuments return zero unexpectedly?
You run
db.orders.countDocuments({ status: 'shipped' }) but it returns 0, even though you know there are shipped orders. Which option explains the most likely cause?Attempts:
2 left
💡 Hint
Filters are case sensitive and must match exactly.
✗ Incorrect
If the
status field values are uppercase but the filter uses lowercase, no documents match, so the count is zero.