MongoDB $eq Operator

What is the $eq operator in MongoDB?

MongoDB provides a variety of comparison query operators. The $eq (equal to) operator is one of those operators. The $eq (equal to) operator is used to match the documents where the field's value is equal to the given value. In other words, the $eq operator is used to specify the condition of equality.

Syntax of the $eq operator:

Or

Examples:

In the following examples, we are working with:

>db.employee.find()
        {
                "_id" : 1,
                "name" : "John",
                "father_name" : "Thomas",
                "department" : "Contain Writting",
                "experience" : 2,
                "language" : { "C#", "python", "java" },
                "mobile_no" : 9856321478,
                "gender" : "Male",
                "salary" : 22000,
                "age" : 26
        }
        {
                "_id" : 2,
                "name" : "William",
                "father_name" : "Rebort",
                "department" : "Softwre Tester",
                "experience" : 1,
                "language" : { "C#", "python" },
                "mobile_no" : 7896541478,
                "gender" : "Male",
                "salary" : 20000,
                "age" : 21
        }
        {
                "_id" : 3,
                "name" : "Ava",
                "father_name" : "William",
                "department" : "Marketing manager",
                "experience" : 5,
                "language" : { "C#", "C++", "java" },
                "mobile_no" : 8789654178,
                "gender" : "Female",
                "salary" : 36500,
                "age" : 25 
        }
        {
                "_id" : 4,
                "name" : "Olivia",
                "father_name" : "Noah",
                "department" : null,
                "experience" : 4,
                "language" : { "C++", "java" },
                "mobile_no" : 9045641478,
                "gender" : "Female",
                "salary" : 30000,
                "age" : 27 
        }
        {
                "_id" : 5,
                "name" : "Elijah",
                "father_name" : "John",
                "department" : "HR",
                "experience" : 0,
                "language" : { "C++", "HTML", "java" },
                "mobile_no" : 6589741230,
                "gender" : "Male",
                "salary" : 15000,
                "age" : 20
        }
        {
                "_id" : 6,
                "name" : "John",
                "father_name" : "Liam",
                "department" : "Softwre Tester",
                "experience" : 10,
                "language" : { "C#", "CSS", "HTML" },
                "mobile_no" : 9014536987,
                "gender" : "Male",
                "salary" : 55000,
                "age" : 30 
        }

Example 1: Using $eq operator

In this example, we are looking for documents whose gender field is equal to male.

Or

Output:

{
    "_id" : 1,
    "name" : "John",
    "father_name" : "Thomas",
    "department" : "Contain Writting",
    "experience" : 2,
    "language" : { "C#", "python", "java" },
    "mobile_no" : 9856321478,
    "gender" : "Male",
    "salary" : 22000,
    "age" : 26
}
{
    "_id" : 2,
    "name" : "William",
    "father_name" : "Rebort",
    "department" : "Softwre Tester",
    "experience" : 1,
    "language" : { "C#", "python"},
    "mobile_no" : 7896541478,
    "gender" : "Male",
    "salary" : 20000,
    "age" : 21
}
{
    "_id" : 5,
    "name" : "Elijah",
    "father_name" : "John",
    "department" : "HR",
    "experience" : 0,
    "language" : { "C++", "HTML", "java" },
    "mobile_no" : 6589741230,
    "gender" : "Male",
    "salary" : 15000,
    "age" : 20
}
{
    "_id" : 6,
    "name" : "John",
    "father_name" : "Liam",
    "department" : "Softwre Tester",
    "experience" : 10,
    "language" : { "C#", "CSS", "HTML" },
    "mobile_no" : 9014536987,
    "gender" : "Male",
    "salary" : 55000,
    "age" : 30 
}

Example 2: Another example

In this example, we're looking for documents that employees work on the "C++" language.

Or

Output:

{
    "_id" : 3,
    "name" : "Ava",
    "father_name" : "William",
    "department" : "Marketing manager",
    "experience" : 5,
    "language" : { "C#", "C++", "java" },
    "mobile_no" : 8789654178,
    "gender" : "Female",
    "salary" : 36500,
    "age" : 25 
}
{
    "_id" : 4,
    "name" : "Olivia",
    "father_name" : "Noah",
    "department" : null,
    "experience" : 4,
    "language" : { "C++", "java" },
    "mobile_no" : 9045641478,
    "gender" : "Female",
    "salary" : 30000,
    "age" : 27 
}
{
    "_id" : 5,
    "name" : "Elijah",
    "father_name" : "John",
    "department" : "HR",
    "experience" : 0,
    "language" : { "C++", "HTML", "java" },
    "mobile_no" : 6589741230,
    "gender" : "Male",
    "salary" : 15000,
    "age" : 20
}





Latest Courses