> For the complete MongoDB documentation index, see www.mongodb.com/docs/llms.txt

# $and (query predicate operator)

[`$and`](https://www.mongodb.com/docs/manual/reference/operator/query/and.md#mongodb-query-op.-and) performs a logical `AND` operation on an array of one or more expressions and selects the documents that satisfy all the expressions.

**Note:**

MongoDB provides an implicit `AND` operation when you specify a comma separated list of expressions.

## Compatibility

You can use `$and` for deployments hosted in the following environments:

- [MongoDB Atlas](https://www.mongodb.com/docs/atlas): The fully managed service for MongoDB deployments in the cloud

* [MongoDB Enterprise](https://www.mongodb.com/docs/manual/administration/install-enterprise.md#std-label-install-mdb-enterprise): The subscription-based, self-managed version of MongoDB

* [MongoDB Community](https://www.mongodb.com/docs/manual/administration/install-community.md#std-label-install-mdb-community-edition): The source-available, free-to-use, and self-managed version of MongoDB

## Syntax

The [`$and`](https://www.mongodb.com/docs/manual/reference/operator/query/and.md#mongodb-query-op.-and) has the following syntax:

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

## Behavior

When evaluating the clauses in the [`$and`](https://www.mongodb.com/docs/manual/reference/operator/query/and.md#mongodb-query-op.-and) expression, MongoDB's query optimizer considers which indexes are available that could help satisfy clauses of the [`$and`](https://www.mongodb.com/docs/manual/reference/operator/query/and.md#mongodb-query-op.-and) expression when [selecting the best plan to execute.](https://www.mongodb.com/docs/manual/core/query-plans.md#std-label-read-operations-query-optimization)

To allow the query engine to optimize queries, `$and` handles errors as follows:

- If any expression supplied to `$and` would cause an error when evaluated alone, the `$and` containing the expression may cause an error but an error is not guaranteed.

- An expression supplied after the first expression supplied to `$and` may cause an error even if the first expression evaluates to `false`.

Most programming languages and drivers, including the [MongoDB Shell](https://www.mongodb.com/docs/mongodb-shell.md#std-label-mdb-shell-overview) (`mongosh`), do not allow the construction of objects with duplicate keys at the same object level. For example:

```javascript
db.inventory.find( { price: { $in: [ 7.99, 3.99 ], $in: [ 4.99, 1.99 ] } } )
```

The previous query is invalid because the field name `price` has duplicate operators at the same object level. The query sent to the server differs from the intent. To make the query work, use an explicit `AND`:

```javascript
db.inventory.find( {
   $and: [
      { price: { $in: [ 7.99, 3.99 ] } },
      { price: { $in: [ 4.99, 1.99 ] } }
   ]
} )
```

The previous query explicitly checks that both conditions are satisfied: the `price` array must include at least one value from each [`$in`](https://www.mongodb.com/docs/manual/reference/operator/query/in.md#mongodb-query-op.-in) set. For more information, see [Examples.](https://www.mongodb.com/docs/manual/reference/operator/query/and.md#std-label-query-and-examples)

## Examples

The examples match multiple expressions on the same field.

Consider this query:

```javascript
db.inventory.find( { $and: [ { price: { $ne: 1.99 } }, { price: { $exists: true } } ] } )
```

The query selects all documents in the `inventory` collection where:

- the `price` field value is not equal to `1.99` **and**

- the `price` field exists.

You can simplify this query by combining the operator expressions for the `price` field into a single query object with a nested implicit `AND`:

```javascript
db.inventory.find( { price: { $ne: 1.99, $exists: true } } )
```

Rewrites are not always possible, particularly when duplicate conditions exist on the same field. For example:

```javascript
db.inventory.find( { status: { $ne: "closed", $ne: "archived" } } )
```

The previous query is invalid because it uses [`$ne`](https://www.mongodb.com/docs/manual/reference/operator/query/ne.md#mongodb-query-op.-ne) more than once on the `status` field at the same object level. Use [`$nin`](https://www.mongodb.com/docs/manual/reference/operator/query/nin.md#mongodb-query-op.-nin) instead:

```javascript
db.inventory.find( { status: { $nin: [ "closed", "archived" ] } } )
```

Rewrite the query based on your intent. Consider this query:

```javascript
db.inventory.find( {
   $and: [
      { status: "new" },
      { status: "processing" }
   ]
} )
```

To find documents where `status` is either `new` or `processing`, use [`$in`:](https://www.mongodb.com/docs/manual/reference/operator/query/in.md#mongodb-query-op.-in)

```javascript
db.inventory.find( { status: { $in: [ "new", "processing" ] } } )
```

If your `status` field is an array `[ "new", "processing" ]` and you want to check if the document contains both `new` and `processing`, use [`$all`:](https://www.mongodb.com/docs/manual/reference/operator/query/all.md#mongodb-query-op.-all)

```javascript
db.inventory.find( { status: { $all: [ "new", "processing" ] } } )
```

The previous query is semantically equivalent to `AND`, but [`$all`](https://www.mongodb.com/docs/manual/reference/operator/query/all.md#mongodb-query-op.-all) is clearer when querying array fields.

Similar to duplicate field names, the same considerations apply for duplicate operators used in the query.

## Learn More

- [`find()`](https://www.mongodb.com/docs/manual/reference/method/db.collection.find.md#mongodb-method-db.collection.find)

- [`$ne`](https://www.mongodb.com/docs/manual/reference/operator/query/ne.md#mongodb-query-op.-ne)

- [`$exists`](https://www.mongodb.com/docs/manual/reference/operator/query/exists.md#mongodb-query-op.-exists)

- [`$in`](https://www.mongodb.com/docs/manual/reference/operator/query/in.md#mongodb-query-op.-in)

- [`$all`](https://www.mongodb.com/docs/manual/reference/operator/query/all.md#mongodb-query-op.-all)
