0
0
MongoDBquery~10 mins

Query patterns that cause collection scans 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 find documents where the field 'age' is exactly 30.

MongoDB
db.users.find({"age": [1])
Drag options to blanks, or click blank then click option'
A{ $eq: 30 }
B"30"
C{ age: 30 }
D30
Attempts:
3 left
💡 Hint
Common Mistakes
Using a string '30' instead of number 30 causes no match if the field is numeric.
Using a nested object incorrectly inside the value.
2fill in blank
medium

Complete the code to find documents where the 'name' field starts with 'A'.

MongoDB
db.users.find({"name": [1])
Drag options to blanks, or click blank then click option'
A/^A/
B"A%"
C{ $regex: 'A' }
D{ $startsWith: 'A' }
Attempts:
3 left
💡 Hint
Common Mistakes
Using SQL-like wildcards such as 'A%' which MongoDB does not understand.
Using incorrect operators like $startsWith which do not exist.
3fill in blank
hard

Fix the error in the query to find documents where 'score' is greater than 50.

MongoDB
db.scores.find({"score": { [1] 50 } })
Drag options to blanks, or click blank then click option'
A$lt:
B$gte:
C$gt:
D$eq:
Attempts:
3 left
💡 Hint
Common Mistakes
Using $gte includes equal values, which is not the same as strictly greater than.
Using $lt or $eq will not find values greater than 50.
4fill in blank
hard

Fill both blanks to create a query that finds documents where 'status' is 'active' and 'age' is less than 40.

MongoDB
db.users.find({"status": [1], "age": { [2] 40 } })
Drag options to blanks, or click blank then click option'
A"active"
B$lt:
C$gt:
D"inactive"
Attempts:
3 left
💡 Hint
Common Mistakes
Using $gt instead of $lt for age.
Using 'inactive' instead of 'active' for status.
5fill in blank
hard

Fill all three blanks to create a query that finds documents where the 'category' is 'books', 'price' is greater than 20, and 'inStock' is true.

MongoDB
db.products.find({"category": [1], "price": { [2] 20 }, "inStock": [3])
Drag options to blanks, or click blank then click option'
A"books"
B$gt:
Ctrue
Dfalse
Attempts:
3 left
💡 Hint
Common Mistakes
Using false instead of true for inStock.
Using $lt instead of $gt for price.