

翻訳は機械翻訳により提供されています。提供された翻訳内容と英語版の間で齟齬、不一致または矛盾がある場合、英語版が優先します。

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

Amazon DocumentDB の \$1indexOfBytes 演算子は、文字列内の部分文字列の開始インデックスを、文字のバイト位置に基づいて検索するために使用されます。これは、Latin 以外のスクリプトなど、複数バイトの文字を含むテキストデータを使用する場合に便利です。

**パラメータ**
+ `string`: 検索する入力文字列。
+ `substring`: 入力文字列内で検索する部分文字列。
+ `[<start>]`: (オプション) 検索の開始位置 (ゼロベース）。指定しない場合、検索は文字列の先頭から始まります。

## 例 (MongoDB シェル)
<a name="indexOfBytes-examples"></a>

次の例は、 を使用して、デスクの場所を表す文字列のセット内の最初のハイフン文字のインデックス`$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 }
```

## コードの例
<a name="indexOfBytes-code"></a>

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

------