

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

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

Amazon DocumentDB의 `$indexOfCP` 연산자는 문자열 표현식 내에서 지정된 하위 문자열이 처음 발생한 인덱스를 코드 포인트(CP)에서 찾는 데 사용됩니다. 이는 문자열 필드에서 콘텐츠를 구문 분석하고 추출할 때 유용할 수 있습니다.

**파라미터**
+ `string expression`: 검색할 문자열입니다.
+ `substring`: 검색할 하위 문자열입니다.
+ `[<start>]`: (선택 사항) 검색을 시작할 위치(제로 기반 인덱스). 기본값은 0.

## 예제(MongoDB 쉘)
<a name="indexOfCP-examples"></a>

이 예제에서는 `$indexOfCP` 연산자를 사용하여 각 문서의 데스크 필드에서 하이픈 문자 -의 첫 번째 발생 인덱스를 찾습니다.

**샘플 문서 생성**

```
db.people.insertMany([
  { "_id":1, "name":"John Doe", "Manager":"Jane Doe", "Role":"Developer", "Desk": "Düsseldorf-BVV-021"},
  { "_id":2, "name":"John Stiles", "Manager":"Jane Doe", "Role":"Manager", "Desk": "Munich-HGG-32a"},
  { "_id":3, "name":"Richard Roe", "Manager":"Jorge Souza", "Role":"Product", "Desk": "Cologne-ayu-892.50"},
  { "_id":4, "name":"Mary Major", "Manager":"Jane Doe", "Role":"Solution Architect", "Desk": "Dortmund-Hop-78"}
])
```

**쿼리 예제**

```
db.people.aggregate([
  { $project: { stateLocation: { $indexOfCP: [ "$Desk", "-"] } } }
])
```

**출력**

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

## 코드 예제
<a name="indexOfCP-code"></a>

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

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

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

async function main() {
  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: { stateLocation: { $indexOfCP: [ "$Desk", "-"] } } }
  ]).toArray();

  console.log(result);

  await client.close();
}

main();
```

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

```
from pymongo import MongoClient

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': { 'stateLocation': { '$indexOfCP': [ '$Desk', '-' ] } } }
]))

print(result)

client.close()
```

------