当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


MongoDB $not用法及代码示例


MongoDB 提供了多种逻辑查询操作符。 $not 运算符就是这些运算符之一。它类似于编程中的逻辑非运算符。 $not 运算符用于对指定的表达式执行逻辑 "NOT operation"。您可以将它与任何其他查询表达式一起使用,例如等于 ($eq)、大于 ($gt) 或小于 ($lt)。

$not 运算符可以与正则表达式一起使用。它仅选择或检索那些与给定运算符表达式不匹配的文档。用户可以根据需要在 find()、update() 等方法中使用该运算符。

用法:

{ field:{ $not:{ operator-expression } } }

例子:

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

Database:JavaTpoint
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 的学生的数据。

db.student.find({"Total_marks":{$not:{$lt:400}}}).pretty()

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 运算符(不大于):

在这个例子中,我们只检索 "age" 不大于 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"
}	




相关用法


注:本文由纯净天空筛选整理自 MongoDB $not Operator。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。