0
0
MongoDBquery~5 mins

$eq for equality in MongoDB

Choose your learning style9 modes available
Introduction
Use $eq to find documents where a field exactly matches a value. It helps you get only the data you want.
When you want to find all users with the name 'Alice'.
When you need to get products priced exactly at 20 dollars.
When filtering orders with a status equal to 'shipped'.
When searching for employees in a specific department by ID.
When checking if a field matches a certain date.
Syntax
MongoDB
{ field: { $eq: value } }
The $eq operator checks if the field is exactly equal to the value.
It can be used inside a find query to filter documents.
Examples
Finds documents where the age field is exactly 30.
MongoDB
{ age: { $eq: 30 } }
Finds documents where the status field is 'active'.
MongoDB
{ status: { $eq: "active" } }
Finds documents where the score field equals 100.
MongoDB
{ score: { $eq: 100 } }
Sample Program
This query finds all users whose age is exactly 25.
MongoDB
db.users.find({ age: { $eq: 25 } })
OutputSuccess
Important Notes
You can also write { field: value } without $eq for simple equality checks.
Using $eq explicitly is helpful when combining with other operators.
Remember $eq matches exact values including type (e.g., number vs string).
Summary
Use $eq to find documents where a field equals a specific value.
It helps filter data precisely in MongoDB queries.
You can write simple equality without $eq, but $eq is clearer in complex queries.