$nor - Amazon DocumentDB

本文為英文版的機器翻譯版本,如內容有任何歧義或不一致之處,概以英文版為準。

$nor

$nor 運算子用於比對沒有任何指定查詢條件為 true 的文件。它類似於邏輯「NOR」操作,如果沒有任何運算元為 true,則結果為 true。

參數

  • expression1:要評估的第一個表達式。

  • expression2:要評估的第二個表達式。

  • expressionN:要評估的其他表達式。

範例 (MongoDB Shell)

下列範例會擷取qty欄位不小於 20 且size欄位不等於「XL」的文件,以示範$nor運算子的使用情況。

建立範例文件

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()