$and

Performs logical AND operation on array of expressions, returning true only if all expressions evaluate to true.

Syntax

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

Parameters

expressionsobjectrequired

Array of expressions to evaluate with logical AND operation

Examples

Combine multiple conditions

Use logical AND to combine multiple boolean conditions

Query:

db.products.aggregate([{ $project: { name: 1, isAvailable: { $and: [{ $gt: ["$quantity", 0] }, { $eq: ["$status", "active"] }] } } }])

Output:

Products with isAvailable field true only when quantity > 0 AND status is active

Multiple field validation

Validate multiple fields meet criteria using AND logic

Query:

db.users.aggregate([{ $project: { username: 1, isValid: { $and: [{ $ne: ["$email", null] }, { $gte: ["$age", 18] }, { $eq: ["$verified", true] }] } } }])

Output:

Users with isValid field indicating all validation criteria are met

Related