0
0
MongoDBquery~20 mins

countDocuments method in MongoDB - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
CountDocuments Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
query_result
intermediate
2:00remaining
Counting documents with a simple filter
Given a MongoDB collection named 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 } })
A0
BError: countDocuments is not a function
C5
DNumber of documents in the collection
Attempts:
2 left
💡 Hint
The countDocuments method counts documents matching the filter.
📝 Syntax
intermediate
2: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' })
Adb.users.countDocuments({ status: 'active' })
Bdb.users.countDocuments({ status == 'active' })
C)} 'evitca' :sutats {(stnemucoDtnuoc.sresu.bd
Ddb.users.countDocuments({ status: {$eq: 'active'} })
Attempts:
2 left
💡 Hint
Filters use colon : to assign values, not double equals ==.
optimization
advanced
2: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?
ACreate an index on the <code>category</code> field before running countDocuments.
BUse <code>countDocuments</code> without any index; MongoDB will optimize automatically.
CUse <code>estimatedDocumentCount</code> instead of <code>countDocuments</code> with a filter.
DRun <code>countDocuments</code> with an empty filter to count all documents.
Attempts:
2 left
💡 Hint
Indexes help MongoDB find matching documents faster.
🧠 Conceptual
advanced
2:00remaining
Difference between countDocuments and estimatedDocumentCount
Which statement correctly describes the difference between countDocuments and estimatedDocumentCount?
ABoth methods always return the exact same count regardless of filters.
B<code>countDocuments</code> is faster than <code>estimatedDocumentCount</code> for large collections.
C<code>estimatedDocumentCount</code> requires a filter; <code>countDocuments</code> does not.
D<code>countDocuments</code> counts documents matching a filter; <code>estimatedDocumentCount</code> returns an approximate total count without a filter.
Attempts:
2 left
💡 Hint
One method can use filters, the other cannot.
🔧 Debug
expert
2: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?
AThe <code>status</code> field values are stored as uppercase strings like 'SHIPPED', so the filter does not match.
BThe <code>countDocuments</code> method only counts documents inserted in the last 24 hours.
CThe collection <code>orders</code> is empty.
DMongoDB does not support filtering with <code>countDocuments</code>.
Attempts:
2 left
💡 Hint
Filters are case sensitive and must match exactly.