$not

Performs logical NOT operation on an expression, returning the opposite boolean value of the input expression.

Syntax

{ $not: [<expression>] }

Parameters

expressionobjectrequired

Expression to negate with logical NOT operation

Examples

Negate boolean condition

Use logical NOT to invert a boolean expression result

Query:

db.products.aggregate([{ $project: { name: 1, isUnavailable: { $not: [{ $gt: ["$quantity", 0] }] } } }])

Output:

Products with isUnavailable field true when quantity is NOT greater than 0

Invert complex condition

Apply NOT operation to complex boolean expression

Query:

db.orders.aggregate([{ $project: { orderId: 1, notCompleted: { $not: [{ $and: [{ $eq: ["$status", "shipped"] }, { $ne: ["$deliveryDate", null] }] }] } } }])

Output:

Orders with notCompleted field indicating NOT (shipped AND delivered)

Related