

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

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

Amazon DocumentDB の `$strcasecmp`演算子は、2 つの文字列間で大文字と小文字を区別しない比較を実行します。2 つの入力文字列の辞書比較を示す整数値が返され、大文字と小文字の違いは無視されます。

**パラメータ**
+ `string1`: 比較する最初の文字列。
+ `string2`: 比較する 2 番目の文字列。

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

この例では、 `$strcasecmp`演算子を使用して`people`コレクション内のデスクの場所文字列を比較し、大文字と小文字の違いを無視する方法を示します。

**サンプルドキュメントを作成する**

```
db.people.insertMany([
  { "_id": 1, "Desk": "mke233-wi" },
  { "_id": 2, "Desk": "MKE233-WI" },
  { "_id": 3, "Desk": "mke233-wi" }
]);
```

**クエリの例**

```
db.people.aggregate([
  {
    $project: {
      item: 1,
      compare: { $strcasecmp: ["$Desk", "mke233-wi"] }
    }
  }
]);
```

**出力**

```
{ "_id" : 1, "compare" : 0 }
{ "_id" : 2, "compare" : 0 }
{ "_id" : 3, "compare" : 0 }
```

出力は、 `&quot;Desk&quot;`フィールドと文字列の比較が 3 つのドキュメントすべて`0`に対して を`&quot;mke233-wi&quot;`返し、大文字と小文字が無視された場合に文字列が等しいことを示します。

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

`$strcasecmp` コマンドを使用するコード例を表示するには、使用する言語のタブを選択します。

------
#### [ 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: {
        item: 1,
        compare: { $strcasecmp: ["$Desk", "mke233-wi"] }
      }
    }
  ]).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(collection.aggregate([
        {
            '$project': {
                'item': 1,
                'compare': { '$strcasecmp': ['$Desk', 'mke233-wi'] }
            }
        }
    ]))

    print(result)

    client.close()

example()
```

------