$and
The $and aggregation operator evaluates multiple expressions and returns true only if all expressions evaluate to true. If any expression is false, it returns false.
Parameters
-
expressions: An array of expressions to evaluate.
Example (MongoDB Shell)
The following example demonstrates using the $and operator to check if products meet multiple criteria.
Create sample documents
db.products.insertMany([ { _id: 1, name: "Laptop", price: 1200, inStock: true }, { _id: 2, name: "Mouse", price: 25, inStock: false }, { _id: 3, name: "Keyboard", price: 75, inStock: true } ]);
Query example
db.products.aggregate([ { $project: { name: 1, price: 1, inStock: 1, affordable: { $and: [ { $lt: ["$price", 100] }, { $eq: ["$inStock", true] } ] } } } ]);
Output
[
{ _id: 1, name: 'Laptop', price: 1200, inStock: true, affordable: false },
{ _id: 2, name: 'Mouse', price: 25, inStock: false, affordable: false },
{ _id: 3, name: 'Keyboard', price: 75, inStock: true, affordable: true }
]
Code examples
To view a code example for using the $and aggregation operator, choose the tab for the language that you want to use: