MongoDB $eq 操作符

MongoDB $eq 操作符

$eq操作符在MongoDB中的作用是什么

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

$eq操作符的语法

{ field : { $eq : value } }

或者

{ field : value }

示例

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

Database: JavaTpoint
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
}

Camera课程

Python教程

Java教程

Web教程

数据库教程

图形图像教程

办公软件教程

Linux教程

计算机教程

大数据教程

开发工具教程