MongoDB $eq 运算符

MongoDB $eq 运算符 介绍

MongoDB 提供了多种比较查询运算符。$eq(等于)运算符就是这些运算符之一。$eq(等于)运算符用于匹配字段值等于给定值的文档。换句话说,$eq 运算符用于指定相等的条件。

MongoDB $eq 运算符 语法

{ field : { $eq : value } }  

或者

{ field : value }  

MongoDB $eq 运算符 例子

在以下示例中,我们正在使用:

Database: Yiidian
Collection: employee  
Document: Six documents that contain the details of the employees. 
>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 
        }

示例 1:使用 $eq 运算符

在这个例子中,我们正在寻找性别字段等于男性的文档。

>db.employee.find({ gender : { $eq : Male }}).pretty()  

或者

>db.employee.find({ gender : Male }).pretty()  

输出结果为:

{
    "_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 
}

示例 2:另一个示例

在此示例中,我们正在查找员工使用“C++”语言工作的文档。

>db.employee.find({ language : { $eq : "C++" }}).pretty()  

或者

>db.employee.find({ language : "C++" }).pretty()  

输出结果为:

{
    "_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
}

 

热门文章

优秀文章