Bird
Raised Fist0
MongoDBquery~20 mins

countDocuments method in MongoDB - Practice Problems & Coding Challenges

Choose your learning style10 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
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.

Practice

(1/5)
1.

What does the countDocuments method do in MongoDB?

easy
A. Counts how many documents match a given filter
B. Deletes documents matching a filter
C. Updates documents matching a filter
D. Returns all documents in a collection

Solution

  1. Step 1: Understand the purpose of countDocuments

    The countDocuments method is used to count documents that match a filter in a collection.
  2. Step 2: Compare with other operations

    Deleting, updating, or returning documents are different operations and not related to counting.
  3. Final Answer:

    Counts how many documents match a given filter -> Option A
  4. Quick Check:

    countDocuments = count matching documents [OK]
Hint: Remember: countDocuments counts, not modifies data [OK]
Common Mistakes:
  • Confusing countDocuments with delete or update methods
  • Thinking it returns the documents instead of a count
  • Assuming it counts all documents without a filter
2.

Which of the following is the correct syntax to count documents with status equal to "active" in a collection named users?

?
easy
A. db.users.countDocuments({ status: "active" })
B. db.users.count({ status: active })
C. db.users.find({ status: "active" }).countDocuments({ status: "active" })
D. db.users.countDocuments("status = active")

Solution

  1. Step 1: Identify correct method usage

    The countDocuments method is called on the collection with a filter object inside parentheses.
  2. Step 2: Check filter format

    The filter must be an object like { status: "active" }, not a string or chained after find().
  3. Final Answer:

    db.users.countDocuments({ status: "active" }) -> Option A
  4. Quick Check:

    Correct syntax uses countDocuments(filter) [OK]
Hint: Use countDocuments with a filter object inside parentheses [OK]
Common Mistakes:
  • Using deprecated count() method
  • Passing filter as a string instead of object
  • Calling countDocuments after find()
3.

Given the collection orders with documents:

[{ "status": "shipped" }, { "status": "pending" }, { "status": "shipped" }]

What will db.orders.countDocuments({ status: "shipped" }) return?

medium
A. 1
B. 0
C. 2
D. 3

Solution

  1. Step 1: Identify documents matching the filter

    Documents with status: "shipped" are the first and third documents.
  2. Step 2: Count matching documents

    There are 2 such documents in total.
  3. Final Answer:

    2 -> Option C
  4. Quick Check:

    Count of shipped orders = 2 [OK]
Hint: Count documents matching filter exactly [OK]
Common Mistakes:
  • Counting all documents instead of filtered ones
  • Misreading the status values
  • Assuming countDocuments returns documents, not count
4.

What is wrong with this code snippet?

const count = db.products.countDocuments("category: 'books'");

It aims to count documents where category is "books".

medium
A. Missing await keyword for asynchronous call
B. The filter is passed as a string instead of an object
C. countDocuments cannot be used on the products collection
D. The method name should be count, not countDocuments

Solution

  1. Step 1: Check filter argument type

    The filter must be an object like { category: 'books' }, not a string.
  2. Step 2: Confirm method usage

    countDocuments is valid on collections and accepts an object filter.
  3. Final Answer:

    The filter is passed as a string instead of an object -> Option B
  4. Quick Check:

    Filter must be an object, not a string [OK]
Hint: Filters are objects, not strings in countDocuments [OK]
Common Mistakes:
  • Passing filter as a string
  • Confusing countDocuments with count
  • Ignoring async/await in some environments (not always error)
5.

You want to count how many users have either age greater than 30 or status equal to "active". Which query correctly uses countDocuments to do this?

hard
A. db.users.countDocuments({ age > 30 || status == "active" })
B. db.users.countDocuments({ age: { $gt: 30 }, status: "active" })
C. db.users.countDocuments({ $and: [ { age: { $gt: 30 } }, { status: "active" } ] })
D. db.users.countDocuments({ $or: [ { age: { $gt: 30 } }, { status: "active" } ] })

Solution

  1. Step 1: Understand the filter logic

    We want documents where age is greater than 30 OR status is "active".
  2. Step 2: Use correct MongoDB query syntax

    The $or operator takes an array of conditions to match either one.
  3. Step 3: Check each option

    The option using { $or: [ { age: { $gt: 30 } }, { status: "active" } ] } is correct. The option with comma-separated conditions uses implicit AND, the one with $and uses explicit AND, and the last uses invalid syntax.
  4. Final Answer:

    db.users.countDocuments({ $or: [ { age: { $gt: 30 } }, { status: "active" } ] }) -> Option D
  5. Quick Check:

    Use $or with array for OR conditions [OK]
Hint: Use $or with array for OR conditions in filters [OK]
Common Mistakes:
  • Using implicit AND instead of OR
  • Writing invalid filter syntax
  • Confusing $and and $or operators