0
0
MongoDBquery~10 mins

Querying array elements directly 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 array 'tags' contains the value 'mongodb'.

MongoDB
db.collection.find({ tags: [1] })
Drag options to blanks, or click blank then click option'
A"tags"
B{ "$in": ["mongodb"] }
C{ "$all": ["mongodb"] }
D"mongodb"
Attempts:
3 left
💡 Hint
Common Mistakes
Using $in or $all unnecessarily when a direct match works.
Quoting the field name instead of the value.
2fill in blank
medium

Complete the code to find documents where the 'scores' array contains the number 85.

MongoDB
db.collection.find({ scores: [1] })
Drag options to blanks, or click blank then click option'
A{ "$elemMatch": { "$eq": 85 } }
B85
C"85"
D{ "$in": [85] }
Attempts:
3 left
💡 Hint
Common Mistakes
Quoting numbers as strings.
Using $elemMatch unnecessarily for a simple value.
3fill in blank
hard

Fix the error in the query to find documents where the 'items' array contains the string 'book'.

MongoDB
db.collection.find({ items: [1] })
Drag options to blanks, or click blank then click option'
A"book"
Bbook
C{ "$eq": "book" }
D{ "$in": "book" }
Attempts:
3 left
💡 Hint
Common Mistakes
Leaving string values unquoted.
Using $in with a string instead of an array.
4fill in blank
hard

Fill both blanks to find documents where the 'ratings' array contains a value greater than 4.

MongoDB
db.collection.find({ ratings: { [1]: [2] } })
Drag options to blanks, or click blank then click option'
A"$gt"
B4
C5
D"$lt"
Attempts:
3 left
💡 Hint
Common Mistakes
Using $lt instead of $gt.
Using a value other than 4.
5fill in blank
hard

Fill all three blanks to find documents where the 'comments' array contains an element with 'author' equal to 'Alice' and 'likes' greater than 10.

MongoDB
db.collection.find({ comments: { [1]: { [2]: "Alice", [3]: 10 } } })
Drag options to blanks, or click blank then click option'
A"$elemMatch"
B"author"
C"likes"
D"$gt"
Attempts:
3 left
💡 Hint
Common Mistakes
Not using $elemMatch for multiple conditions.
Mixing up field names or operators.