MongoDB $ne 运算符

MongoDB $ne 运算符 介绍

MongoDB 提供了多种比较查询运算符。$ne(不等于)运算符就是这些运算符之一。$ne(不等于)运算符用于选择字段值不等于给定值的文档。您可以根据您的要求在更新、查找等方法中使用此运算符。

MongoDB $ne 运算符 语法

{ field : { $ne : value } }  

MongoDB $ne 运算符 例子

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

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:使用 $ne 运算符

在此示例中,我们只寻找男性员工。换句话说,我们正在寻找性别不等于女性的文件。

>db.employee.find({ gender : { $ne : Female }}).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:另一个示例

在此示例中,我们仅选择薪水字段值不等于 25000 的员工。

>db.employee.find({ salary : { $ne : 25000 }}).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" : 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 
}

 

热门文章

优秀文章