MongoDB $not 运算符
MongoDB $not 运算符 介绍
MongoDB 提供了多种逻辑查询运算符。$not 运算符就是这些运算符之一。它类似于编程中的逻辑非运算符。$not 运算符用于对指定的表达式执行逻辑“NOT 运算”。您可以将它与任何其他查询表达式一起使用,例如等于 ($eq)、大于 ($gt) 或小于 ($lt)。
$not 运算符可以与正则表达式一起使用。它只选择或检索那些与给定运算符表达式不匹配的文档。用户可以根据需要在 find()、update() 等方法中使用此运算符。
MongoDB $not 运算符 语句
{ field: { $not: { operator-expression } } }
MongoDB $not 运算符 例子
在以下示例中,我们正在使用:
Database: Yiidian
Collection: student
Document: Six documents that contain the details of the students
[
{
"std_name" : "Jack",
"sex" : "Male",
"class" : "VI",
"age" : 11,
"Total_marks" : 303
"Result" : "Pass"
},
{
"std_name" : "Jenny",
"sex" : "Female",
"class" : "VI",
"age" : 13,
"Total_marks" : 800
"Result" : "Pass"
},
{
"std_name" : "Thomas",
"sex" : "Male",
"class" : "V",
"age" : 11,
" Total_marks" : 200
" Result" : "Fail"
},
{
"std_name" : "Lassy",
"sex" : "Female",
"class" : "X",
"age" : 17,
"Total_marks" : 550
"Result" : "Pass"
},
{
"std_name" : "Mia",
"sex" : "Female",
"class" : "X",
"age" : 19,
" Total_marks" : 350
"Result" : "Pass"
},
{
"std_name" : "Mike,
"sex" : "Male",
"class" : "V",
"age" : 16,
"Total_marks" : 700
"Result" : "Pass"
}
]
示例 1:MongoDB 逻辑 $not 运算符(至少):
在此示例中,我们仅检索“Total_marks”至少为 400 的学生的数据。
效果类似于下面的SQL语句:
SELECT *
FROM student
WHERE Total_marks >= 400;
输出结果为:
>db.student.find({"Total_marks" : {$not: {$lt : 400}}}).pretty()
{
"_Id" : ObjectId("4565d4cd85d4f1vf6345612"),
"std_name" : "Jenny",
"sex" : "Female",
"class" : "VI",
"age" : 13,
"Total_marks" : 800
"Result" : "Pass"
}
{
"_Id" : ObjectId("4565d4cd85d4f1vf6345665"),
"std_name" : "Lassy",
"sex" : "Female",
"class" : "X",
"age" : 17,
"Total_marks" : 550
"Result" : "Pass"
}
{
"_Id" : ObjectId("4565d4cd85d4f1vf6345756"),
"std_name" : "Mike,
"sex" : "Male",
"class" : "V",
"age" : 16,
"Total_marks" : 700
"Result" : "Pass"
}
示例 2:MongoDB 逻辑 $not 运算符(不大于):
在这个例子中,我们只检索“年龄”不大于 12 的学生的数据。
db.student.find({ "age" : {$ not : {$gt : 12}}}).pretty()
输出结果为:
>db.student.find({"age" : {$not: {$gt : 12}}}).pretty()
{
"_Id" : ObjectId("4565d4cd85d4f545ffg43df7"),
"std_name" : "Jack",
"sex" : "Male",
"class" : "VI",
"age" : 11,
"Total_marks" : 303
"Result" : "Pass"
}
{
"_Id" : ObjectId("4565d4cd85d4f545ffg43df7"),
"std_name" : "Thomas",
"sex" : "Male",
"class" : "V",
"age" : 11,
" Total_marks" : 200
" Result" : "Fail"
}
示例 3:MongoDB 逻辑 $not 运算符(不等于):
在此示例中,我们仅检索“age”不等于 11 的学生的数据。
db.student.find({ "age" : {$ not : {$eq : 11}}}).pretty()
>db.student.find({"age" : {$not: {$eq : 11}}}).pretty()
{
"_Id" : ObjectId("4565d4cd85d45d4dgf1fd5f4"),
"std_name" : "Jenny",
"sex" : "Female",
"class" : "VI",
"age" : 13,
"Total_marks" : 800
"Result" : "Pass"
}
{
"_Id" : ObjectId("4565d4cd85d45d4dgf1fd55s"),
"std_name" : "Lassy",
"sex" : "Female",
"class" : "X",
"age" : 17,
"Total_marks" : 550
"Result" : "Pass"
}
{
"_Id" : ObjectId("4565d4cd85d45d4d85331531"),
"std_name" : "Mia",
"sex" : "Female",
"class" : "X",
"age" : 19,
" Total_marks" : 350
"Result" : "Pass"
}
{
"_Id" : ObjectId("4565d4cd85d45d4456c53154"),
"std_name" : "Mike,
"sex" : "Male",
"class" : "V",
"age" : 16,
"Total_marks" : 700
"Result" : "Pass"
}
热门文章
优秀文章