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

# $encStrContains (expression operator)

## Definition

**New in version 8.2**

**Note:**

The `$encStrContains` aggregation operator is only for encrypted fields in collections with [Queryable Encryption](https://www.mongodb.com/docs/manual/core/queryable-encryption.md#std-label-qe-manual-feature-qe) enabled. For unencrypted fields, use [Text Search](https://www.mongodb.com/docs/manual/text-search.md#std-label-text-search) operators to match substrings.

Returns `true` if a subset of characters in a string value match the characters in the specified string. The queried field must have [substring queries enabled](https://www.mongodb.com/docs/manual/core/queryable-encryption/fundamentals/encrypt-and-query.md#std-label-qe-encryption-schema), and the length of the query string must be between the configured minimum and maximum number of characters, inclusive.

By default, strings must match case and diacritical marks.

- Set [`caseSensitive`](https://www.mongodb.com/docs/manual/core/queryable-encryption/fundamentals/encrypt-and-query.md#mongodb-parameter-param.caseSensitive) to `false` in the encryption schema for case-insensitive matching.

- Set [`diacriticSensitive`](https://www.mongodb.com/docs/manual/core/queryable-encryption/fundamentals/encrypt-and-query.md#mongodb-parameter-param.diacriticSensitive) to `false` in the encryption schema to disregard diacritic variations when matching.

The [`$encStrContains`](https://www.mongodb.com/docs/manual/reference/operator/aggregation/encstrcontains.md#mongodb-expression-exp.-encStrContains) expression has the following [operator expression syntax:](https://www.mongodb.com/docs/manual/reference/mql/expressions.md#std-label-aggregation-expressions)

```javascript
{ $encStrContains: { input: ’$fieldname’, substring: <target search key> } }
```

## Behavior

- Searches match whitespace characters.

- Line breaks aren't considered when matching.

- Tokenization delimiters aren't supported.

## Example

In [`mongosh`:](https://www.mongodb.com/docs/mongodb-shell.md#mongodb-binary-bin.mongosh)

```shell
db.collection('MyCollection').aggregate([
   {
      $match: {
         $expr: {
            $encStrContains: {
               input: '$employeeLastName',
               substring: 'earso'
            }
         }
      }
   }
])
```

To match multiple fields:

```shell
db.collection('MyCollection').aggregate([
   {
      $match: {
         $expr: {
            $and: [
               {
                  $encStrContains: {
                     input: '$employeeLastName',
                     substring: 'earso'
                  }
               },
               {
                  $encStrContains: {
                     input: '$employeeFirstName',
                     substring: 'John'
                  }
               }
            ]
         }
      }
   }
])
```
