MongoDB提供了不同类型的逻辑查询运算符,而$nor
运算符就是其中之一。此运算符用于在一个或多个表达式的数组上执行逻辑NOR操作,并仅选择或检索与该数组中所有给定表达式都不匹配的那些文档。您可以根据需要在find(),update()等方法中使用此运算符。
句法:
{ $nor: [ { Expression1 }, { Expression2 }, ... { ExpressionN } ] }
在以下示例中,我们正在使用:
Database: GeeksforGeeks
Collection: contributor
Document: three documents that contain the details of the contributors in the form of field-value pairs.
使用$nor
运算符匹配值:
在此示例中,我们仅检索薪水不是3000且分支机构不是ECE的那些雇员的文档。
db.contributor.find({$nor: [{salary: 3000}, {branch: "ECE"}]}).pretty()
使用$nor
运算符匹配嵌套/嵌入文档中的值:
在此示例中,我们仅检索那些年龄不为24岁且状态不是AP的员工文档。
db.contributor.find({$nor: [{"personal.age": 24},
{"personal.state": "AP"}]}).pretty()
使用$nor
运算符匹配数组中的值:
在此示例中,我们仅检索与给定数组不匹配的那些雇员的文档。
db.contributor.find({$nor: [{language: {$in: ["Java", "C++"]}}]}).pretty()