

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

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

`$strLenCP`Operator di Amazon DocumentDB digunakan untuk menentukan panjang ekspresi string di titik kode (karakter Unicode). Ini berguna ketika Anda perlu mengetahui jumlah karakter dalam string, bukan jumlah byte.

**Parameter**
+ `expression`: Ekspresi string untuk mengembalikan panjang dalam poin kode.

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

Contoh berikut menunjukkan penggunaan `$strLenCP` operator untuk menentukan panjang string dengan karakter Unicode.

**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": { $strLenCP: "$Desk" }
    }
  }
])
```

**Keluaran**

```
{ "_id" : 1, "Desk" : "Düsseldorf-BVV-021", "length" : 18 }
{ "_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 perbedaan pengukuran panjang untuk string “Düsseldorf-BVV-021", yang berisi karakter Unicode (Ü). `$strLenCP`Operator menghitung jumlah karakter Unicode dengan benar, sedangkan `$strLenBytes` operator menghitung jumlah byte.

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

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

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

Berikut adalah contoh penggunaan `$strLenCP` operator dalam aplikasi Node.js dengan driver MongoDB:

```
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": { $strLenCP: "$Desk" }
      }
    }
  ]).toArray();

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

example();
```

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

Berikut adalah contoh penggunaan `$strLenCP` operator dalam aplikasi Python dengan driver: PyMongo 

```
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": { "$strLenCP": "$Desk" }
            }
        }
    ]))

    print(result)
    client.close()

example()
```

------