$not - Amazon DocumentDB

Terjemahan disediakan oleh mesin penerjemah. Jika konten terjemahan yang diberikan bertentangan dengan versi bahasa Inggris aslinya, utamakan versi bahasa Inggris.

$not

Operator $not agregasi melakukan operasi NOT logis pada ekspresi. Ia kembali true jika ekspresi mengevaluasifalse, dan false jika ekspresi mengevaluasi untuk. true

Parameter

  • expression: Ekspresi untuk meniadakan.

Contoh (MongoDB Shell)

Contoh berikut menunjukkan menggunakan $not operator untuk membalikkan nilai boolean.

Buat dokumen sampel

db.users.insertMany([ { _id: 1, name: "Alice", active: true }, { _id: 2, name: "Bob", active: false }, { _id: 3, name: "Charlie", active: true } ]);

Contoh kueri

db.users.aggregate([ { $project: { name: 1, active: 1, inactive: { $not: ["$active"] } } } ]);

Keluaran

[ { _id: 1, name: 'Alice', active: true, inactive: false }, { _id: 2, name: 'Bob', active: false, inactive: true }, { _id: 3, name: 'Charlie', active: true, inactive: false } ]

Contoh kode

Untuk melihat contoh kode untuk menggunakan operator $not agregasi, pilih tab untuk bahasa yang ingin Anda gunakan:

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('users'); const result = await collection.aggregate([ { $project: { name: 1, active: 1, inactive: { $not: ["$active"] } } } ]).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['users'] result = list(collection.aggregate([ { '$project': { 'name': 1, 'active': 1, 'inactive': { '$not': ['$active'] } } } ])) print(result) client.close() example()