

本文為英文版的機器翻譯版本，如內容有任何歧義或不一致之處，概以英文版為準。

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

Amazon DocumentDB 中的`$strcasecmp`運算子會在兩個字串之間執行不區分大小寫的比較。它會傳回整數值，指出兩個輸入字串的語彙比較，忽略大小寫差異。

**參數**
+ `string1`：要比較的第一個字串。
+ `string2`：要比較的第二個字串。

## 範例 (MongoDB Shell)
<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;` 欄位與字串之間的比較`&quot;mke233-wi&quot;`會`0`傳回所有三個文件，表示忽略大小寫時字串相等。

## 程式碼範例
<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()
```

------