$strLenBytes - Amazon DocumentDB

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

$strLenBytes

Amazon DocumentDB의 $strLenBytes 연산자는 문자열의 길이를 바이트 단위로 결정하는 데 사용됩니다. 이는 문자열 필드의 스토리지 크기를 이해해야 할 때, 특히 문자당 1바이트 이상을 사용할 수 있는 유니코드 문자를 처리할 때 유용합니다.

파라미터

  • expression: 길이를 계산할 문자열 표현식입니다.

예제(MongoDB 쉘)

이 예제에서는 $strLenBytes 연산자를 사용하여 문자열 필드의 길이를 바이트 단위로 계산하는 방법을 보여줍니다.

샘플 문서 생성

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

출력

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

"Düsseldorf-BVV-021" 문자열의 길이는 19바이트이며, 이는 2바이트를 차지하는 유니코드 문자 ""로 인한 코드 포인트(18) 수와 다릅니다.

코드 예제

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

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