$indexOfBytes - Amazon DocumentDB

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

$indexOfBytes

Amazon DocumentDB의 $indexOfBytes 연산자는 문자의 바이트 위치에 따라 문자열 내에서 하위 문자열의 시작 인덱스를 찾는 데 사용됩니다. 이는 비 라틴어 스크립트에 있는 것과 같이 멀티바이트 문자를 포함할 수 있는 텍스트 데이터로 작업할 때 유용할 수 있습니다.

파라미터

  • string: 검색할 입력 문자열입니다.

  • substring: 입력 문자열 내에서 검색할 하위 문자열입니다.

  • [<start>]: (선택 사항) 검색의 시작 위치(0 기반)입니다. 지정하지 않으면 문자열의 시작 부분에서 검색이 시작됩니다.

예제(MongoDB 쉘)

다음 예제에서는를 사용하여 데스크 위치를 나타내는 문자열 집합에서 첫 번째 하이픈 문자의 인덱스를 찾는 $indexOfBytes 방법을 보여줍니다.

샘플 문서 생성

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: { stateLocation: { $indexOfBytes: ["$Desk", "-"] } } } ]);

출력

{ "_id" : 1, "stateLocation" : 11 } { "_id" : 2, "stateLocation" : 6 } { "_id" : 3, "stateLocation" : 7 } { "_id" : 4, "stateLocation" : 8 }

코드 예제

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

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 events = db.collection('people'); const result = await db.collection('people').aggregate([ { $project: { stateLocation: { $indexOfBytes: ["$Desk", "-"] } } } ]).toArray(); console.log(result); await 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(db.people.aggregate([ { '$project': { 'stateLocation': { '$indexOfBytes': ['$Desk', '-'] } } } ])) print(result) client.close() example()