$or

Performs logical OR operation on array of expressions, returning true if any expression evaluates to true.

Syntax

{ $or: [<expression1>, <expression2>, ...] }

Parameters

expressionsobjectrequired

Array of expressions to evaluate with logical OR operation

Examples

Alternative conditions

Use logical OR to check if any of several conditions are true

Query:

db.products.aggregate([{ $project: { name: 1, isPromoted: { $or: [{ $eq: ["$onSale", true] }, { $eq: ["$featured", true] }, { $gt: ["$discount", 0] }] } } }])

Output:

Products with isPromoted field true if on sale OR featured OR has discount

Multiple status check

Check if document matches any of several status values

Query:

db.orders.aggregate([{ $project: { orderId: 1, needsAttention: { $or: [{ $eq: ["$status", "pending"] }, { $eq: ["$status", "cancelled"] }] } } }])

Output:

Orders with needsAttention field true for pending OR cancelled status

Related