Search⌘ K
AI Features

Solution: Using Logical Operators

We'll cover the following...
Query
db.orders.find({
$and: [
{ $or: [{ status: "Pending" }, { status: "Processing" }] },
{ total: { $gt: 500 } }
]
}).sort({ orderDate: -1 }).limit(3)

The explanation of the query is given below:

  • Line 1: This begins a query (a read operation) that will search documents in orders matching the filter object provided between the braces { ... }.

  • Line 2: This uses the $and logical operator. It takes an array of subfilters and requires all of them to be true for a document to match. In this query, there are two conditions inside the array; both must be satisfied.

  • Line 3: This is the first element of the $and array: an $or condition.

    • ...