Recall & Review
beginner
What does the
countDocuments() method do in MongoDB?It counts the number of documents in a collection that match a given filter or query.
Click to reveal answer
beginner
How do you count all documents in a collection using
countDocuments()?Call
countDocuments({}) with an empty filter object to count all documents.Click to reveal answer
intermediate
What is the difference between
count() and countDocuments() in MongoDB?countDocuments() counts documents matching a filter accurately, while count() is deprecated and less precise with some query types.Click to reveal answer
beginner
Write a simple example to count documents where the field
status equals "active".db.collection.countDocuments({ status: "active" })
Click to reveal answer
intermediate
Can
countDocuments() be used with complex filters like ranges or logical operators?Yes, you can use any valid MongoDB query filter with
countDocuments(), including ranges, $and, $or, and others.Click to reveal answer
What does
countDocuments({}) return?✗ Incorrect
countDocuments({}) counts all documents in the collection because the filter is empty.
Which method is recommended for counting documents in MongoDB?
✗ Incorrect
countDocuments() is the recommended method for accurate document counts.
What type of argument does
countDocuments() accept?✗ Incorrect
countDocuments() takes a query filter object to specify which documents to count.
If you want to count documents where
age is greater than 30, which filter is correct?✗ Incorrect
The $gt operator means 'greater than', so { age: { $gt: 30 } } counts documents with age over 30.
What does
countDocuments({ status: "active" }) return?✗ Incorrect
This counts only documents where the status field equals 'active'.
Explain how to use the
countDocuments() method to find the number of documents matching a condition.Think about how you tell MongoDB what to count.
You got /4 concepts.
Describe the difference between
countDocuments() and the deprecated count() method.Focus on why one is preferred over the other.
You got /4 concepts.