📜  MongoDB - 逻辑查询运算符

📅  最后修改于: 2022-05-13 01:56:58.331000             🧑  作者: Mango

MongoDB - 逻辑查询运算符

MongoDB 支持逻辑查询运算符。这些运算符用于过滤数据并根据给定条件获得精确的结果。下表包含比较查询运算符:

OperatorDescription
$andIt is used to join query clauses with a logical AND and return all documents that match the given conditions of both clauses.
$orIt is used to join query clauses with a logical OR and return all documents that match the given conditions of either clause.
$notIt is used to invert the effect of the query expressions and return documents that does not match the query expression.
$norIt is used to join query clauses with a logical NOR and return all documents that fail to match both clauses.

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

Database: GeeksforGeeks
Collection: contributor
Document: three documents that contain the details of the contributors in the form of field-value pairs.

使用$and运算符匹配值:

在此示例中,我们仅检索分支为 CSE 且 joinYear 为 2018 的员工文档。

db.contributor.find({$and: [{branch: "CSE"}, {joiningYear: 2018}]}).pretty()



使用$nor运算符匹配值:

在此示例中,我们仅检索工资不是 3000 且分支不是 ECE 的员工文档。

db.contributor.find({$nor: [{salary: 3000}, {branch: "ECE"}]}).pretty()

使用$or运算符匹配值:

在此示例中,我们仅检索分支为 ECE 或 joinYear 为 2017 的员工文档。

db.contributor.find({$or: [{branch: "ECE"}, {joiningYear: 2017}]}).pretty()

使用$not运算符匹配值:

在此示例中,我们仅检索薪水不大于 2000 的员工文档。

db.contributor.find({salary: {$not: {$gt: 2000}}}).pretty()