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.
Jump into concepts and practice - no test required
db.collection.find({ age: { $eq: 30 } })| Step | Document | Field Value (age) | Condition (age == 30) | Result |
|---|---|---|---|---|
| 1 | { name: 'Alice', age: 25 } | 25 | 25 == 30? False | Exclude |
| 2 | { name: 'Bob', age: 30 } | 30 | 30 == 30? True | Include |
| 3 | { name: 'Carol', age: 35 } | 35 | 35 == 30? False | Exclude |
| 4 | { name: 'Dave', age: 30 } | 30 | 30 == 30? True | Include |
| 5 | No more documents | - | - | Stop |
| Variable | Start | After 1 | After 2 | After 3 | After 4 | Final |
|---|---|---|---|---|---|---|
| Current Document | None | { name: 'Alice', age: 25 } | { name: 'Bob', age: 30 } | { name: 'Carol', age: 35 } | { name: 'Dave', age: 30 } | None |
| age | N/A | 25 | 30 | 35 | 30 | N/A |
| Condition Result | N/A | False | True | False | True | N/A |
| Included in Result | N/A | No | Yes | No | Yes | N/A |
$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.$eq operator do in a MongoDB query?$eq$eq operator is used to filter documents where a field exactly matches a given value.$eq.$eq means equality match [OK]age equals 30 using $eq?$eq as key and the value to match as value: { age: { $eq: 30 } }.users with documents:{ "name": "Alice", "age": 25 }, { "name": "Bob", "age": 30 }, { "name": "Carol", "age": 30 }db.users.find({ age: { $eq: 30 } }) return?age equals 30.age 30, so both documents match and will be returned.db.products.find({ price: $eq: 100 }) but it gives an error. What is wrong?$eq operator must be inside an object as the value for the field key, like { price: { $eq: 100 } }.$eq: 100, causing a syntax error.$eq: 100 value -> Option Borders where the status is exactly "shipped" and the quantity is 10. Which query correctly uses $eq for both conditions?$eq operator inside an object: { status: { $eq: "shipped" } } and { quantity: { $eq: 10 } }.