0
0
MongoDBquery~10 mins

countDocuments method 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 'users' collection.

MongoDB
const count = await db.collection('users').[1]();
Drag options to blanks, or click blank then click option'
Afind
BcountDocuments
CinsertOne
DdeleteMany
Attempts:
3 left
💡 Hint
Common Mistakes
Using find() instead of countDocuments() to count documents.
Trying to use insertOne() which adds documents, not counts them.
2fill in blank
medium

Complete the code to count documents where the field 'status' equals 'active'.

MongoDB
const activeCount = await db.collection('users').countDocuments({ [1]: 'active' });
Drag options to blanks, or click blank then click option'
Astatus
Bage
Cname
Demail
Attempts:
3 left
💡 Hint
Common Mistakes
Using a wrong field name that does not exist in documents.
Leaving the query object empty when a filter is needed.
3fill in blank
hard

Fix the error in the code to correctly count documents with age greater than 30.

MongoDB
const count = await db.collection('users').countDocuments({ age: { [1]: 30 } });
Drag options to blanks, or click blank then click option'
A$gt
B$lt
C$eq
D$ne
Attempts:
3 left
💡 Hint
Common Mistakes
Using $lt which means 'less than' instead of 'greater than'.
Using $eq which means 'equal to' instead of a comparison operator.
4fill in blank
hard

Fill both blanks to count documents where 'score' is at least 80 and 'passed' is true.

MongoDB
const passedCount = await db.collection('exams').countDocuments({ score: { [1]: 80 }, [2]: true });
Drag options to blanks, or click blank then click option'
A$gte
Bpassed
Cscore
D$lte
Attempts:
3 left
💡 Hint
Common Mistakes
Using $lte instead of $gte for the score comparison.
Using wrong field names for the boolean condition.
5fill in blank
hard

Fill all three blanks to count documents where 'category' is 'books', 'price' is less than 20, and 'inStock' is true.

MongoDB
const cheapBooksCount = await db.collection('products').countDocuments({ [1]: 'books', price: { [2]: 20 }, [3]: true });
Drag options to blanks, or click blank then click option'
Acategory
B$lt
CinStock
Dprice
Attempts:
3 left
💡 Hint
Common Mistakes
Mixing up field names like using 'price' for category.
Using $gt instead of $lt for price comparison.
Using 'stock' instead of 'inStock' for availability.