![]() |
VOOZH | about |
The MongoDB $eq operator matches documents where a field value exactly equals a specified value, and is commonly used for precise filtering in queries, updates, and aggregation pipelines.
MongoDB allows two ways to use the $eq operator:
1. Explicit Syntax
{field: {$eq: value}}2. Implicit Syntax
{field: value}Here are some features discussed:
Examples demonstrate how to filter documents based on exact matches using $eq. We will be working with:
Retrieving those documents where the value of the salary field is equal to 30000.
Query:
db.employee.find({salary: {$eq: 30000}})or using implicit syntax:
db.employee.find({salary: 30000})Output:
Retrieving those documents where the first name of the employee is equal to Daniel. We are specifying conditions on the field in the embedded document using dot notation in this example.
Query:
db.employee.find({"name.first": {$eq: "Daniel"}})Or using implicit syntax:
db.employee.find({"name.first": "Daniel"})Output:
Retrieving those documents where the language field matches the value "C++" (for arrays, this matches if the array contains the value).
Query:
db.employee.find({language: {$eq: "C++"}})Or using implicit syntax:
db.employee.find({language: "C++"})Output:
Retrieving those documents where the language array is equal to the specified array.
Query:
db.employee.find({ language: { $eq: ["C#", "Java"] } })Or using implicit syntax:
db.employee.find({ language: ["C#", "Java"] })Output: