MongoDB $ne 操作符
$ne
操作符在MongoDB中表示什么
MongoDB提供了多种比较查询操作符。$ne
(不等于)操作符是其中之一。$ne
(不等于)操作符用于选择字段值不等于给定值的文档。根据需求,您可以在更新、查找和类似的方法中使用该操作符。
$ne 操作符的语法
{ field : { $ne : 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:使用$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
}