$eq for equality in MongoDB - Time & Space Complexity
Start learning this pattern below
Jump into concepts and practice - no test required
When we use $eq in MongoDB, we want to find documents where a field matches a specific value.
We ask: How does the time to find these documents grow as the collection gets bigger?
Analyze the time complexity of the following code snippet.
db.collection.find({ age: { $eq: 25 } })
This query finds all documents where the age field equals 25.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Checking each document's
agefield to see if it equals 25. - How many times: Once for each document in the collection if no index is used.
As the collection grows, the number of documents to check grows too.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 checks |
| 100 | 100 checks |
| 1000 | 1000 checks |
Pattern observation: The work grows directly with the number of documents.
Time Complexity: O(n)
This means the time to find matching documents grows in a straight line with the collection size.
[X] Wrong: "Using $eq always finds results instantly no matter the collection size."
[OK] Correct: Without an index, MongoDB must check each document one by one, so bigger collections take more time.
Understanding how simple equality checks scale helps you explain database query performance clearly and confidently.
"What if we added an index on the age field? How would the time complexity change?"
Practice
$eq operator do in a MongoDB query?Solution
Step 1: Understand the purpose of
The$eq$eqoperator 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 DQuick Check:
$eqmeans equality match [OK]
- Confusing $eq with sorting or updating operators
- Thinking $eq modifies data instead of filtering
- Assuming $eq works for inequality
age equals 30 using $eq?Solution
Step 1: Recall MongoDB query syntax for $eq
The correct syntax uses the field name as key and an object with$eqas 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 AQuick Check:
Field: { $eq: value } format [OK]
- Placing $eq outside the field key
- Using double colons or wrong operators
- Using JavaScript equality syntax instead of MongoDB
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?Solution
Step 1: Understand the query condition
The query looks for documents whereageequals 30.Step 2: Check documents matching the condition
Bob and Carol haveage30, so both documents match and will be returned.Final Answer:
[{ "name": "Bob", "age": 30 }, { "name": "Carol", "age": 30 }] -> Option AQuick Check:
age == 30 returns Bob and Carol [OK]
- Returning documents with age not equal to 30
- Expecting only one document instead of all matches
- Thinking $eq causes syntax error
db.products.find({ price: $eq: 100 }) but it gives an error. What is wrong?Solution
Step 1: Check the query syntax
The$eqoperator 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: 100value -> Option BQuick Check:
Use { field: { $eq: value } } syntax [OK]
- Omitting braces around $eq operator
- Assuming $eq works without object syntax
- Blaming collection or field name incorrectly
orders where the status is exactly "shipped" and the quantity is 10. Which query correctly uses $eq for both conditions?Solution
Step 1: Use $eq for each field separately
Each field must have its own$eqoperator inside an object:{ status: { $eq: "shipped" } }and{ quantity: { $eq: 10 } }.Step 2: Combine conditions in one query object
Put both conditions inside the find query object to filter documents matching both.Final Answer:
db.orders.find({ status: { $eq: "shipped" }, quantity: { $eq: 10 } }) -> Option CQuick Check:
Use separate $eq for each field [OK]
- Trying to use one $eq for multiple fields
- Using JavaScript equality operators in query
- Omitting $eq and relying on implicit equality
