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

# $strLenBytes (expression operator)

## Definition

Returns the number of UTF-8 encoded bytes in the specified string.

[`$strLenBytes`](https://www.mongodb.com/docs/manual/reference/operator/aggregation/strlenbytes.md#mongodb-expression-exp.-strLenBytes) has the following [operator expression syntax:](https://www.mongodb.com/docs/manual/reference/mql/expressions.md#std-label-agg-quick-ref-operator-expressions)

```javascript
{ $strLenBytes: <string expression> }
```

The argument can be any valid [expression](https://www.mongodb.com/docs/manual/reference/mql/expressions.md#std-label-aggregation-expressions) as long as it resolves to a string. For more information on expressions, see [Expressions.](https://www.mongodb.com/docs/manual/reference/mql/expressions.md#std-label-aggregation-expressions)

If the argument resolves to a value of `null` or refers to a missing field, [`$strLenBytes`](https://www.mongodb.com/docs/manual/reference/operator/aggregation/strlenbytes.md#mongodb-expression-exp.-strLenBytes) returns an error.

## Behavior

The [`$strLenBytes`](https://www.mongodb.com/docs/manual/reference/operator/aggregation/strlenbytes.md#mongodb-expression-exp.-strLenBytes) operator counts the number of UTF-8 encoded bytes in a string where each character may use between one and four bytes.

For example, US-ASCII characters are encoded using one byte. Characters with diacritic markings and additional Latin alphabetical characters (Latin characters outside of the English alphabet) are encoded using two bytes. Chinese, Japanese and Korean characters typically require three bytes, and other planes of unicode (emoji, mathematical symbols, etc.) require four bytes.

The [`$strLenBytes`](https://www.mongodb.com/docs/manual/reference/operator/aggregation/strlenbytes.md#mongodb-expression-exp.-strLenBytes) operator differs from [`$strLenCP`](https://www.mongodb.com/docs/manual/reference/operator/aggregation/strlencp.md#mongodb-expression-exp.-strLenCP) operator which counts the [code points](http://www.unicode.org/glossary/#code_point) in the specified string regardless of how many bytes each character uses.

| Example | Results | Notes |
| --- | --- | --- |
| `{ $strLenBytes: "abcde" }` | `5` | Each character is encoded using one byte. |
| `{ $strLenBytes: "Hello World!" }` | `12` | Each character is encoded using one byte. |
| `{ $strLenBytes: "cafeteria" }` | `9` | Each character is encoded using one byte. |
| `{ $strLenBytes: "cafétéria" }` | `11` | `é` is encoded using two bytes. |
| `{ $strLenBytes: "" }` | `0` | Empty strings return 0. |
| `{ $strLenBytes: "$€λG" }` | `7` | `€` is encoded using three bytes. `λ` is encoded using two bytes. |
| `{ $strLenBytes: "寿司" }` | `6` | Each character is encoded using three bytes. |

## Example

### Single-Byte and Multibyte Character Set

Create a `food` collection with the following documents:

```javascript
db.food.insertMany(
 [
    { "_id" : 1, "name" : "apple" },
    { "_id" : 2, "name" : "banana" },
    { "_id" : 3, "name" : "éclair" },
    { "_id" : 4, "name" : "hamburger" },
    { "_id" : 5, "name" : "jalapeño" },
    { "_id" : 6, "name" : "pizza" },
    { "_id" : 7, "name" : "tacos" },
    { "_id" : 8, "name" : "寿司" }
 ]
)
```

The following operation uses the `$strLenBytes` operator to calculate the `length` of each `name` value:

```javascript
db.food.aggregate(
  [
    {
      $project: {
        "name": 1,
        "length": { $strLenBytes: "$name" }
      }
    }
  ]
)
```

The operation returns the following results:

```javascript
{ "_id" : 1, "name" : "apple", "length" : 5 }
{ "_id" : 2, "name" : "banana", "length" : 6 }
{ "_id" : 3, "name" : "éclair", "length" : 7 }
{ "_id" : 4, "name" : "hamburger", "length" : 9 }
{ "_id" : 5, "name" : "jalapeño", "length" : 9 }
{ "_id" : 6, "name" : "pizza", "length" : 5 }
{ "_id" : 7, "name" : "tacos", "length" : 5 }
{ "_id" : 8, "name" : "寿司", "length" : 6 }
```

The documents with `_id: 3` and `_id: 5` each contain a diacritic character (`é` and `ñ` respectively) that requires two bytes to encode. The document with `_id: 8` contains two Japanese characters that are encoded using three bytes each.  This makes the `length` greater than the number of characters in `name` for the documents with `_id: 3`, `_id: 5` and `_id: 8`.

**See also:**

- [`$strLenCP`](https://www.mongodb.com/docs/manual/reference/operator/aggregation/strlencp.md#mongodb-expression-exp.-strLenCP)

- [`$binarySize`](https://www.mongodb.com/docs/manual/reference/operator/aggregation/binarysize.md#mongodb-expression-exp.-binarySize)
