MongoDB $not 运算符

MongoDB $not 运算符

MongoDB 提供了多种逻辑查询运算符,$not 运算符是其中之一。它类似于编程中的逻辑 NOT 运算符。$not 运算符用于在指定表达式上执行逻辑的“非”操作。您可以将它与任何其他查询表达式一起使用,例如等于($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(不大于):

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

Camera课程

Python教程

Java教程

Web教程

数据库教程

图形图像教程

办公软件教程

Linux教程

计算机教程

大数据教程

开发工具教程