Amazon Web Services 文档中描述的 Amazon Web Services 服务或功能可能因区域而异。要查看适用于中国区域的差异,请参阅
中国的 Amazon Web Services 服务入门
(PDF)。
本文属于机器翻译版本。若本译文内容与英语原文存在差异,则一律以英文原文为准。
$nor
该$nor运算符用于匹配所有指定查询条件均不为真的文档。它类似于逻辑 “NOR” 运算,如果所有操作数都不为真,则结果为真。
参数
-
expression1:要计算的第一个表达式。
-
expression2:要计算的第二个表达式。
-
expressionN:其他要评估的表达式。
示例(MongoDB 外壳)
以下示例通过检索字段不小于 20 且qty字段不等于 “XL” 的文档来演示$nor运算符的用法。size
创建示例文档
db.items.insertMany([
{ qty: 10, size: "M" },
{ qty: 15, size: "XL" },
{ qty: 25, size: "L" },
{ qty: 30, size: "XL" }
])
查询示例
db.items.find({
$nor: [
{ qty: { $lt: 20 } },
{ size: "XL" }
]
})
输出
[
{ "_id" : ObjectId("..."), "qty" : 25, "size" : "L" }
]
代码示例
要查看使用该$nor命令的代码示例,请选择要使用的语言的选项卡:
- Node.js
-
const { MongoClient } = require('mongodb');
async function example() {
const client = await MongoClient.connect('mongodb://<username>:<password>@<cluster-endpoint>:27017/?tls=true&tlsCAFile=global-bundle.pem&replicaSet=rs0&readPreference=secondaryPreferred&retryWrites=false');
const db = client.db('test');
const collection = db.collection('items');
const result = await collection.find({
$nor: [
{ qty: { $lt: 20 } },
{ size: "XL" }
]
}).toArray();
console.log(result);
await client.close();
}
example();
- Python
-
from pymongo import MongoClient
def example():
client = MongoClient('mongodb://<username>:<password>@<cluster-endpoint>:27017/?tls=true&tlsCAFile=global-bundle.pem&replicaSet=rs0&readPreference=secondaryPreferred&retryWrites=false')
db = client['test']
collection = db['items']
result = list(collection.find({
'$nor': [
{ 'qty': { '$lt': 20 } },
{ 'size': 'XL' }
]
}))
print(result)
client.close()
example()