0
0
MongoDBquery~10 mins

$eq for equality in MongoDB - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - $eq for equality
Start Query
Check each document
Compare field value with $eq value
Include
Return matched documents
The query checks each document's field value against the $eq value. If equal, the document is included in the result.
Execution Sample
MongoDB
db.collection.find({ age: { $eq: 30 } })
Finds all documents where the age field equals 30.
Execution Table
StepDocumentField Value (age)Condition (age == 30)Result
1{ name: 'Alice', age: 25 }2525 == 30? FalseExclude
2{ name: 'Bob', age: 30 }3030 == 30? TrueInclude
3{ name: 'Carol', age: 35 }3535 == 30? FalseExclude
4{ name: 'Dave', age: 30 }3030 == 30? TrueInclude
5No more documents--Stop
💡 All documents checked, query returns only those with age equal to 30.
Variable Tracker
VariableStartAfter 1After 2After 3After 4Final
Current DocumentNone{ name: 'Alice', age: 25 }{ name: 'Bob', age: 30 }{ name: 'Carol', age: 35 }{ name: 'Dave', age: 30 }None
ageN/A25303530N/A
Condition ResultN/AFalseTrueFalseTrueN/A
Included in ResultN/ANoYesNoYesN/A
Key Moments - 2 Insights
Why does the document with age 25 get excluded even though it has an age field?
Because the $eq operator checks for exact equality. In the execution_table row 1, 25 == 30 is False, so the document is excluded.
What happens if a document does not have the age field?
It will be excluded because the field value is undefined and does not equal 30, similar to the False condition in the execution_table.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, which document is included at step 2?
A{ name: 'Bob', age: 30 }
B{ name: 'Alice', age: 25 }
C{ name: 'Carol', age: 35 }
D{ name: 'Dave', age: 30 }
💡 Hint
Check the 'Result' column at step 2 in the execution_table.
At which step does the condition age == 30 become false for the first time?
AStep 2
BStep 1
CStep 3
DStep 4
💡 Hint
Look at the 'Condition' column in the execution_table for step 1.
If the query was changed to { age: { $eq: 35 } }, which document would be included at step 3?
A{ name: 'Alice', age: 25 }
B{ name: 'Bob', age: 30 }
C{ name: 'Carol', age: 35 }
D{ name: 'Dave', age: 30 }
💡 Hint
Refer to the 'Field Value' and 'Condition' columns in the execution_table at step 3.
Concept Snapshot
$eq operator checks if a field's value equals a specified value.
Syntax: { field: { $eq: value } }
Returns documents where field == value.
Useful for exact matches in queries.
If field missing or not equal, document excluded.
Full Transcript
The $eq operator in MongoDB queries checks if a document's field value exactly matches a given value. The query examines each document one by one. For each document, it compares the field's value to the $eq value. If they are equal, the document is included in the results; otherwise, it is excluded. For example, querying { age: { $eq: 30 } } returns only documents where the age field is exactly 30. Documents with different ages or missing the age field are not included. This step-by-step process ensures only matching documents are returned.