

Terjemahan disediakan oleh mesin penerjemah. Jika konten terjemahan yang diberikan bertentangan dengan versi bahasa Inggris aslinya, utamakan versi bahasa Inggris.

# \$1strLenBytes
<a name="strLenBytes"></a>

`$strLenBytes`Operator di Amazon DocumentDB digunakan untuk menentukan panjang string dalam byte. Ini berguna ketika Anda perlu memahami ukuran penyimpanan bidang string, terutama ketika berhadapan dengan karakter Unicode yang mungkin menggunakan lebih dari satu byte per karakter.

**Parameter**
+ `expression`: Ekspresi string untuk menghitung panjang.

## Contoh (MongoDB Shell)
<a name="strLenBytes-examples"></a>

Contoh ini menunjukkan bagaimana menggunakan `$strLenBytes` operator untuk menghitung panjang bidang string dalam byte.

**Buat dokumen sampel**

```
db.people.insertMany([
  { "_id": 1, "Desk": "Düsseldorf-BVV-021" },
  { "_id": 2, "Desk": "Munich-HGG-32a" },
  { "_id": 3, "Desk": "Cologne-ayu-892.50" },
  { "_id": 4, "Desk": "Dortmund-Hop-78" }
]);
```

**Contoh kueri**

```
db.people.aggregate([
  {
    $project: {
      "Desk": 1,
      "length": { $strLenBytes: "$Desk" }
    }
  }
])
```

**Keluaran**

```
{ "_id" : 1, "Desk" : "Düsseldorf-BVV-021", "length" : 19 }
{ "_id" : 2, "Desk" : "Munich-HGG-32a", "length" : 14 }
{ "_id" : 3, "Desk" : "Cologne-ayu-892.50", "length" : 18 }
{ "_id" : 4, "Desk" : "Dortmund-Hop-78", "length" : 15 }
```

Perhatikan bahwa panjang string “Düsseldorf-BVV-021" adalah 19 byte, yang berbeda dari jumlah titik kode (18) karena karakter Unicode “Ü” menempati 2 byte.

## Contoh kode
<a name="strLenBytes-code"></a>

Untuk melihat contoh kode untuk menggunakan `$strLenBytes` perintah, pilih tab untuk bahasa yang ingin Anda gunakan:

------
#### [ Node.js ]

```
const { MongoClient } = require('mongodb');

async function example() {
  const client = await MongoClient.connect('mongodb://<username>:<password>@<cluster-endpoint>:27017/?tls=true&tlsCAFile=global-bundle.pem&replicaSet=rs0&readPreference=secondaryPreferred&retryWrites=false');
  const db = client.db('test');
  const collection = db.collection('people');

  const result = await collection.aggregate([
    {
      $project: {
        "Desk": 1,
        "length": { $strLenBytes: "$Desk" }
      }
    }
  ]).toArray();

  console.log(result);
  client.close();
}

example();
```

------
#### [ Python ]

```
from pymongo import MongoClient

def example():
    client = MongoClient('mongodb://<username>:<password>@<cluster-endpoint>:27017/?tls=true&tlsCAFile=global-bundle.pem&replicaSet=rs0&readPreference=secondaryPreferred&retryWrites=false')
    db = client.test
    collection = db.people

    result = list(collection.aggregate([
        {
            '$project': {
                "Desk": 1,
                "length": { "$strLenBytes": "$Desk" }
            }
        }
    ]))

    print(result)
    client.close()

example()
```

------