翻訳は機械翻訳により提供されています。提供された翻訳内容と英語版の間で齟齬、不一致または矛盾がある場合、英語版が優先します。
$strcasecmp
Amazon DocumentDB の $strcasecmp演算子は、2 つの文字列間で大文字と小文字を区別しない比較を実行します。2 つの入力文字列の辞書比較を示す整数値が返され、大文字と小文字の違いは無視されます。
パラメータ
-
string1: 比較する最初の文字列。
-
string2: 比較する 2 番目の文字列。
例 (MongoDB シェル)
この例では、 $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 }
出力は、 "Desk"フィールドと文字列の比較が 3 つのドキュメントすべて0に対して を"mke233-wi"返し、大文字と小文字が無視された場合に文字列が等しいことを示します。
コードの例
$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()