$and
The $and operator in Amazon DocumentDB is used to combine multiple expressions and evaluate them as a single condition. It returns true if all the provided expressions evaluate to true, and false otherwise. This operator is useful for applying multiple criteria to a query.
Parameters
-
expression1: A required expression that evaluates to a boolean value. -
expression2: A required expression that evaluates to a boolean value. -
...: Additional required expressions that evaluate to boolean values.
Example (MongoDB Shell)
The following example demonstrates the use of the $and operator to find all documents in the "users" collection where the "age" field is greater than 18 and the "status" field is "active".
Create sample documents
db.users.insertMany([ { name: "John", age: 25, status: "active" }, { name: "Jane", age: 17, status: "active" }, { name: "Bob", age: 30, status: "inactive" }, { name: "Alice", age: 22, status: "active" } ]);
Query example
db.users.find({ $and: [ { age: { $gt: 18 } }, { status: "active" } ] });
Output
[
{ "_id" : ObjectId("614e3c4b63f5892e7c4e2345"), "name" : "John", "age" : 25, "status" : "active" },
{ "_id" : ObjectId("614e3c4b63f5892e7c4e2347"), "name" : "Alice", "age" : 22, "status" : "active" }
]
Code examples
To view a code example for using the $and command, choose the tab for the language that you want to use: