Bird
Raised Fist0
MongoDBquery~20 mins

$eq for equality in MongoDB - Practice Problems & Coding Challenges

Choose your learning style10 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Challenge - 5 Problems
🎖️
MongoDB $eq Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
query_result
intermediate
2:00remaining
Find documents where age equals 30
Given a collection users with documents containing an age field, which query returns all users whose age is exactly 30?
MongoDB
db.users.find({ age: { $eq: 30 } })
Adb.users.find({ age: { $eq: "30" } })
Bdb.users.find({ age: { $eq: 30 } })
Cdb.users.find({ age: 30 })
Ddb.users.find({ $eq: { age: 30 } })
Attempts:
2 left
💡 Hint
Use $eq to match the exact value and type.
📝 Syntax
intermediate
2:00remaining
Identify the syntax error in $eq usage
Which of the following MongoDB queries has a syntax error when using $eq?
Adb.collection.find({ $eq: { field: 10 } })
Bdb.collection.find({ field: { $eq: "value" } })
Cdb.collection.find({ field: { $eq: 10 } })
Ddb.collection.find({ field: 10 })
Attempts:
2 left
💡 Hint
Check the position of $eq operator in the query.
query_result
advanced
2:00remaining
Find documents with nested field equal to a value
Given documents with a nested field address.city, which query returns documents where city equals "New York"?
Adb.collection.find({ address: { city: { $eq: "New York" } } })
Bdb.collection.find({ "address.city": "New York" })
Cdb.collection.find({ "address.city": { $eq: "New York" } })
Ddb.collection.find({ $eq: { "address.city": "New York" } })
Attempts:
2 left
💡 Hint
Use dot notation to query nested fields with $eq.
optimization
advanced
2:00remaining
Optimize query using $eq for exact match
Which query is the most efficient to find documents where status equals "active"?
Adb.collection.find({ status: { $in: ["active"] } })
Bdb.collection.find({ status: { $eq: "active" } })
Cdb.collection.find({ $eq: { status: "active" } })
Ddb.collection.find({ status: "active" })
Attempts:
2 left
💡 Hint
Consider the simplest form for exact equality in MongoDB.
🧠 Conceptual
expert
3:00remaining
Understanding $eq behavior with different data types
What will be the result of this query if the collection has documents with score as both string and number types?

db.collection.find({ score: { $eq: 10 } })
AReturns documents where score is the number 10 only
BReturns documents where score is the string "10" only
CReturns documents where score is either number 10 or string "10"
DReturns no documents because of type mismatch
Attempts:
2 left
💡 Hint
MongoDB $eq matches both value and type exactly.

Practice

(1/5)
1. What does the $eq operator do in a MongoDB query?
easy
A. It sorts documents in ascending order.
B. It deletes documents from the collection.
C. It updates documents with new values.
D. It matches documents where a field is equal to a specified value.

Solution

  1. Step 1: Understand the purpose of $eq

    The $eq operator is used to filter documents where a field exactly matches a given value.
  2. Step 2: Compare with other options

    Sorting, updating, and deleting are different operations and not related to $eq.
  3. Final Answer:

    It matches documents where a field is equal to a specified value. -> Option D
  4. Quick Check:

    $eq means equality match [OK]
Hint: Remember: $eq means 'equals' in queries [OK]
Common Mistakes:
  • Confusing $eq with sorting or updating operators
  • Thinking $eq modifies data instead of filtering
  • Assuming $eq works for inequality
2. Which of the following is the correct syntax to find documents where the field age equals 30 using $eq?
easy
A. { age: { $eq: 30 } }
B. { $eq: { age: 30 } }
C. { age: $eq: 30 }
D. { age == 30 }

Solution

  1. Step 1: Recall MongoDB query syntax for $eq

    The correct syntax uses the field name as key and an object with $eq as key and the value to match as value: { age: { $eq: 30 } }.
  2. Step 2: Identify incorrect syntax

    Options B, C, and D do not follow MongoDB query syntax rules and will cause errors.
  3. Final Answer:

    { age: { $eq: 30 } } -> Option A
  4. Quick Check:

    Field: { $eq: value } format [OK]
Hint: Use { field: { $eq: value } } for equality [OK]
Common Mistakes:
  • Placing $eq outside the field key
  • Using double colons or wrong operators
  • Using JavaScript equality syntax instead of MongoDB
3. Given the collection users with documents:
{ "name": "Alice", "age": 25 }, { "name": "Bob", "age": 30 }, { "name": "Carol", "age": 30 }

What will the query db.users.find({ age: { $eq: 30 } }) return?
medium
A. [{ "name": "Bob", "age": 30 }, { "name": "Carol", "age": 30 }]
B. [] (empty array)
C. [{ "name": "Alice", "age": 25 }]
D. Syntax error

Solution

  1. Step 1: Understand the query condition

    The query looks for documents where age equals 30.
  2. Step 2: Check documents matching the condition

    Bob and Carol have age 30, so both documents match and will be returned.
  3. Final Answer:

    [{ "name": "Bob", "age": 30 }, { "name": "Carol", "age": 30 }] -> Option A
  4. Quick Check:

    age == 30 returns Bob and Carol [OK]
Hint: Look for documents where field equals value [OK]
Common Mistakes:
  • Returning documents with age not equal to 30
  • Expecting only one document instead of all matches
  • Thinking $eq causes syntax error
4. You wrote the query db.products.find({ price: $eq: 100 }) but it gives an error. What is wrong?
medium
A. Field name price is invalid
B. Missing curly braces around $eq: 100 value
C. You cannot use $eq with numbers
D. The collection name products is incorrect

Solution

  1. Step 1: Check the query syntax

    The $eq operator must be inside an object as the value for the field key, like { price: { $eq: 100 } }.
  2. Step 2: Identify the missing braces

    The query is missing curly braces around $eq: 100, causing a syntax error.
  3. Final Answer:

    Missing curly braces around $eq: 100 value -> Option B
  4. Quick Check:

    Use { field: { $eq: value } } syntax [OK]
Hint: Always wrap $eq and value in braces { } [OK]
Common Mistakes:
  • Omitting braces around $eq operator
  • Assuming $eq works without object syntax
  • Blaming collection or field name incorrectly
5. You want to find documents in orders where the status is exactly "shipped" and the quantity is 10. Which query correctly uses $eq for both conditions?
hard
A. db.orders.find({ status: "shipped", quantity: 10 })
B. db.orders.find({ $eq: { status: "shipped", quantity: 10 } })
C. db.orders.find({ status: { $eq: "shipped" }, quantity: { $eq: 10 } })
D. db.orders.find({ status == "shipped", quantity == 10 })

Solution

  1. Step 1: Use $eq for each field separately

    Each field must have its own $eq operator inside an object: { status: { $eq: "shipped" } } and { quantity: { $eq: 10 } }.
  2. Step 2: Combine conditions in one query object

    Put both conditions inside the find query object to filter documents matching both.
  3. Final Answer:

    db.orders.find({ status: { $eq: "shipped" }, quantity: { $eq: 10 } }) -> Option C
  4. Quick Check:

    Use separate $eq for each field [OK]
Hint: Use separate $eq for each field in the query [OK]
Common Mistakes:
  • Trying to use one $eq for multiple fields
  • Using JavaScript equality operators in query
  • Omitting $eq and relying on implicit equality