MongoDB $nor 运算符

MongoDB $nor 运算符 介绍

MongoDB 提供了多种逻辑查询运算符。$nor 运算符就是这些运算符之一。$nor 运算符用于对一个或多个表达式的数组执行逻辑“NOR 运算”。换句话说,$nor 是一个逻辑查询运算符,它允许用户对一个或多个查询表达式的数组执行逻辑 NOR 运算。此运算符还用于选择或检索不匹配数组中所有给定表达式的文档。用户可以根据需要在 find()、update() 等方法中使用此运算符。

MongoDB $nor 运算符 语法

{ $nor: [ { Expression1 }, { Expression2 }, ... { ExpressionN } ] }

MongoDB $nor 运算符 例子

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

Database: Yiidian
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",
                     }           
}

示例 2:使用 $nor 运算符匹配两个值

在此示例中,我们仅检索课程不是 btech 且 batch_year 不是 2017 的学生的数据。

>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",
                     }           
}

 

热门文章

优秀文章