MongoDB $nor
运算符 ($nor
)
MongoDB 提供了各种逻辑查询运算符。$nor
运算符是其中之一。$nor
运算符用于在一个或多个表达式的数组上执行逻辑的 “NOR 操作”。换句话说,$nor
是一个逻辑查询运算符,它允许用户在一个或多个查询表达式的数组上执行逻辑的 NOR 操作。此运算符还用于选择或检索与数组中的所有给定表达式不匹配的文档。用户可以根据自己的需求在find()、update()等方法中使用此运算符。
语法
{ $nor: [ { Expression1 }, { Expression2 }, ... { ExpressionN } ] }
示例
在以下示例中,我们正在使用:
Database: JavaTpoint
Collection: student
Document: Five documents that contain the details of the students
>db.student.find()
{
"_id" : ObjectId("56254d4fdf2222265r4g12ds3d65f"),
"name" : "Mick",
"Course" : "btech",
"batch_year" : 2018,
"language" : ["c++", "java", "python"],
"personal_details" :
{
"Father_name" : "Jonny",
"phone_no" : 8895321456,
"age" : 23,
"gender" : "Male",
"City" : "NewYork",
}
}
{
"_id" : ObjectId("56254d4fdf2222265r4g12ds3d691"),
"name" : "Zoya",
"Course" : "BCA",
"batch_year" : 2020,
"language" : ["C#", "JavaScript"],
"personal_details" :
{
"Father_name" : "Henry",
"phone_no" : 9874563698,
"age" : 20,
"gender" : "Female",
"City" : "London",
}
}
{
"_id" : ObjectId("56254d4fdf2222265r4g12ds3d655"),
"name" : "Jonny",
"Course" : "MCA",
"batch_year" : 2019,
"language" : ["C#", "java", "PHP"],
"personal_details" :
{
"Father_name" : "Thomas",
"phone_no" : 7845123698,
"age" : 24,
"gender" : "Male",
"City" : "London",
}
}
{
"_id" : ObjectId("56254d4fdf2222265r4g12ds3d678"),
"name" : "Oliver",
"Course" : "BA",
"batch_year" : 2017,
"language" : ["c", "PHP"],
"personal_details" :
{
"Father_name" : "William",
"phone_no" : 9997845123,
"age" : 25,
"gender" : "Male",
"City" : "Liverpool",
}
}
{
"_id" : ObjectId("56254d4fdf2222265r4g12ds3d665"),
"name" : "Mia",
"Course" : "btech",
"batch_year" : 2020,
"language" : ["HTML", "CSS", "PHP"],
"personal_details" :
{
"Father_name" : "Leo",
"phone_no" : 6312547896,
"age" : 22,
"gender" : "Female",
"City" : "Manchester",
}
}
示例1:使用$nor
运算符匹配值
在这个示例中,我们只检索那些课程不是btech的学生的数据。
>db.student.find({$nor: [{Course: "btech"}]}).pretty()
输出:
>db.student.find({$nor: [{Course: "btech"}]}).pretty()
{
"_id" : ObjectId("56254d4fdf2222265r4g12ds3d691"),
"name" : "Zoya",
"Course" : "BCA",
"batch_year" : 2020,
"language" : ["C#", "JavaScript"],
"personal_details" :
{
"Father_name" : "Henry",
"phone_no" : 9874563698,
"age" : 20,
"gender" : "Female",
"City" : "London",
}
}
{
"_id" : ObjectId("56254d4fdf2222265r4g12ds3d655"),
"name" : "Jonny",
"Course" : "MCA",
"batch_year" : 2019,
"language" : ["C#", "java", "PHP"],
"personal_details" :
{
"Father_name" : "Thomas",
"phone_no" : 7845123698,
"age" : 24,
"gender" : "Male",
"City" : "London",
}
}
{
"_id" : ObjectId("56254d4fdf2222265r4g12ds3d678"),
"name" : "Oliver",
"Course" : "BA",
"batch_year" : 2017,
"language" : ["c", "PHP"],
"personal_details" :
{
"Father_name" : "William",
"phone_no" : 9997845123,
"age" : 25,
"gender" : "Male",
"City" : "Liverpool",
}
}
>db.student.find({$nor: [{Course: "btech"}, {batch_year: "2017"}]}).pretty()
输出:
>db.student.find({$nor: [{Course: "btech"}, {batch_year: "2017"}]}).pretty()
{
"_id" : ObjectId("56254d4fdf2222265r4g12ds3d691"),
"name" : "Zoya",
"Course" : "BCA",
"batch_year" : 2020,
"language" : ["C#", "JavaScript"],
"personal_details" :
{
"Father_name" : "Henry",
"phone_no" : 9874563698,
"age" : 20,
"gender" : "Female",
"City" : "London",
}
}
{
"_id" : ObjectId("56254d4fdf2222265r4g12ds3d655"),
"name" : "Jonny",
"Course" : "MCA",
"batch_year" : 2019,
"language" : ["C#", "java", "PHP"],
"personal_details" :
{
"Father_name" : "Thomas",
"phone_no" : 7845123698,
"age" : 24,
"gender" : "Male",
"City" : "London",
}
}
示例3:使用$nor运算符匹配数组中的值
在这个示例中,我们只检索不匹配给定数组的学生数据。
> db.student.find({nor: [{language: {in: ["Java", "C#"]}}]}).pretty()
输出:
> db.student.find({nor: [{language: {in: ["Java", "C#"]}}]}).pretty()
{
"_id" : ObjectId("56254d4fdf2222265r4g12ds3d678"),
"name" : "Oliver",
"Course" : "BA",
"batch_year" : 2017,
"language" : ["c", "PHP"],
"personal_details" :
{
"Father_name" : "William",
"phone_no" : 9997845123,
"age" : 25,
"gender" : "Male",
"City" : "Liverpool",
}
}
{
"_id" : ObjectId("56254d4fdf2222265r4g12ds3d665"),
"name" : "Mia",
"Course" : "btech",
"batch_year" : 2020,
"language" : ["HTML", "CSS", "PHP"],
"personal_details" :
{
"Father_name" : "Leo",
"phone_no" : 6312547896,
"age" : 22,
"gender" : "Female",
"City" : "Manchester",
}
}
示例4:MongoDB逻辑$nor运算符(检索嵌入文档中的数据)
在这个示例中,我们只获取城市不是“伦敦”并且年龄不是22岁的学生的数据。
>db.student.find({$nor: [{"personal.age": 22}, {"personal.city": "London"}]}).pretty()
输出:
>db.student.find({$nor: [{"personal.age": 22}, {"personal.city": "London"}]}).pretty()
{
"_id" : ObjectId("56254d4fdf2222265r4g12ds3d65f"),
"name" : "Mick",
"Course" : "btech",
"batch_year" : 2018,
"language" : ["c++", "java", "python"],
"personal_details" :
{
"Father_name" : "Jonny",
"phone_no" : 8895321456,
"age" : 23,
"gender" : "Male",
"City" : "NewYork",
}
}
{
"_id" : ObjectId("56254d4fdf2222265r4g12ds3d678"),
"name" : "Oliver",
"Course" : "BA",
"batch_year" : 2017,
"language" : ["c", "PHP"],
"personal_details" :
{
"Father_name" : "William",
"phone_no" : 9997845123,
"age" : 25,
"gender" : "Male",
"City" : "Liverpool",
}
}