Complete the code to count all documents in the 'users' collection.
const count = await db.collection('users').[1]();
The countDocuments method returns the number of documents in the collection.
Complete the code to count documents where the field 'status' equals 'active'.
const activeCount = await db.collection('users').countDocuments({ [1]: 'active' });
The query object specifies the field to filter documents. Here, we count documents with status equal to 'active'.
Fix the error in the code to correctly count documents with age greater than 30.
const count = await db.collection('users').countDocuments({ age: { [1]: 30 } });
The MongoDB query operator $gt means 'greater than'. It filters documents where age is greater than 30.
Fill both blanks to count documents where 'score' is at least 80 and 'passed' is true.
const passedCount = await db.collection('exams').countDocuments({ score: { [1]: 80 }, [2]: true });
$gte means 'greater than or equal to', used to check if score is at least 80. The field passed is checked for true.
Fill all three blanks to count documents where 'category' is 'books', 'price' is less than 20, and 'inStock' is true.
const cheapBooksCount = await db.collection('products').countDocuments({ [1]: 'books', price: { [2]: 20 }, [3]: true });
The query filters documents where category equals 'books', price is less than 20 using $lt, and inStock is true.