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
$eq for equality in MongoDB queries
📖 Scenario: You are managing a small library database. You want to find books by a specific author.
🎯 Goal: Build a MongoDB query using the $eq operator to find all books where the author matches a given name.
📋 What You'll Learn
Create a collection named books with sample book documents
Define a variable authorName with the exact author name to search
Write a MongoDB query using $eq to find books by authorName
Complete the query with a projection to show only the title and author fields
💡 Why This Matters
🌍 Real World
Finding specific records in a database by matching exact values is common in apps like libraries, stores, and user management systems.
💼 Career
Knowing how to use <code>$eq</code> helps you filter data efficiently in MongoDB, a popular NoSQL database used in many companies.
Progress0 / 4 steps
1
Create the books collection with sample data
Create a variable called books that is a list of three documents with these exact entries: { title: "The Hobbit", author: "J.R.R. Tolkien" }, { title: "1984", author: "George Orwell" }, and { title: "Animal Farm", author: "George Orwell" }.
MongoDB
Hint
Use a list of dictionaries to represent the collection.
2
Define the author name to search
Create a variable called authorName and set it to the string "George Orwell".
MongoDB
Hint
Set the variable exactly as shown.
3
Write the MongoDB query using $eq
Create a variable called query and set it to a dictionary that uses $eq to match the author field to the variable authorName. The query should look like { "author": { "$eq": authorName } }.
MongoDB
Hint
Use the $eq operator inside the query dictionary.
4
Add the projection to show only title and author
Create a variable called projection and set it to a dictionary that includes "title": 1 and "author": 1 to show only these fields in the query result.
MongoDB
Hint
Use 1 to include fields in the projection.
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
Step 1: Understand the purpose of $eq
The $eq operator is used to filter documents where a field exactly matches a given value.
Step 2: Compare with other options
Sorting, updating, and deleting are different operations and not related to $eq.
Final Answer:
It matches documents where a field is equal to a specified value. -> Option D
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
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 } }.
Step 2: Identify incorrect syntax
Options B, C, and D do not follow MongoDB query syntax rules and will cause errors.
Final Answer:
{ age: { $eq: 30 } } -> Option A
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
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
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 } }.
Step 2: Identify the missing braces
The query is missing curly braces around $eq: 100, causing a syntax error.
Final Answer:
Missing curly braces around $eq: 100 value -> Option B
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 } })