$strLenCP - Amazon DocumentDB

기계 번역으로 제공되는 번역입니다. 제공된 번역과 원본 영어의 내용이 상충하는 경우에는 영어 버전이 우선합니다.

$strLenCP

Amazon DocumentDB의 $strLenCP 연산자는 코드 포인트(유니코드 문자)의 문자열 표현식 길이를 결정하는 데 사용됩니다. 이는 바이트 수가 아닌 문자열의 문자 수를 알아야 할 때 유용합니다.

파라미터

  • expression: 코드 포인트의 길이를 반환할 문자열 표현식입니다.

예제(MongoDB 쉘)

다음 예제에서는 $strLenCP 연산자를 사용하여 유니코드 문자가 있는 문자열의 길이를 결정하는 방법을 보여줍니다.

샘플 문서 생성

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" } ])

쿼리 예제

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

출력

{ "_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 }

유니코드 문자(")가 포함된 "Düsseldorf-BVV-021" 문자열의 길이 측정 차이를 확인합니다. $strLenCP 연산자는 유니코드 문자 수를 올바르게 계산하고 $strLenBytes 연산자는 바이트 수를 계산합니다.

코드 예제

$strLenCP 명령을 사용하기 위한 코드 예제를 보려면 사용하려는 언어의 탭을 선택합니다.

Node.js

다음은 MongoDB 드라이버와 함께 Node.js 애플리케이션에서 $strLenCP 연산자를 사용하는 예입니다.

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

다음은 PyMongo 드라이버와 함께 Python 애플리케이션에서 $strLenCP 연산자를 사용하는 예입니다.

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()