MongoDB $not 运算符2024 年 9 月 6 日 | 3 分钟阅读 MongoDB 提供了各种逻辑查询运算符。 $not 运算符是其中一个运算符。 它类似于编程中的逻辑 NOT 运算符。 $not 运算符用于对指定的表达式执行逻辑“NOT 操作”。 您可以将它与任何其他查询表达式一起使用,例如等于 ($eq)、大于 ($gt) 或小于 ($lt)。 $not 运算符可以与正则表达式一起使用。 它仅选择或检索与给定运算符表达式不匹配的文档。 用户可以根据他们的要求在 find()、update() 等方法中使用此运算符。 语法示例在下面的示例中,我们将使用 示例 1:MongoDB 逻辑 $not 运算符(至少) 在此示例中,我们仅检索“Total_marks”至少为 400 的学生的数据。 SQL 等效命令 输出 >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() { "_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() { "_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 $or 运算符 |
我们请求您订阅我们的新闻通讯以获取最新更新。