$not - Amazon DocumentDB
Services or capabilities described in Amazon Web Services documentation might vary by Region. To see the differences applicable to the China Regions, see Getting Started with Amazon Web Services in China (PDF).

$not

The $not aggregation operator performs a logical NOT operation on an expression. It returns true if the expression evaluates to false, and false if the expression evaluates to true.

Parameters

  • expression: The expression to negate.

Example (MongoDB Shell)

The following example demonstrates using the $not operator to invert boolean values.

Create sample documents

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

Query example

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

Output

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

Code examples

To view a code example for using the $not aggregation operator, choose the tab for the language that you want to use:

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