MongoDBで特定の文字で終わる値を持つドキュメントを取得する方法
MongoDBで、フィールドの値が特定の文字で終わっているドキュメントを検索したい場合、$regex演算子と正規表現の行末アンカー「$」を組み合わせます。本記事では、サンプルデータを作成しながら、具体的な検索手順と実行結果を分かりやすく解説します。
基本構文
値の末尾が特定の文字で終わるドキュメントを取得するための基本的な構文は以下の通りです。
db.yourCollectionName.find({yourFieldName: {$regex: "yourEndingCharacter$"}}).pretty();
正規表現パターンの最後に $ を付けることで、「指定した文字で終わる」値に一致するドキュメントのみを抽出できます。逆に先頭一致させたい場合は「^」を使用します。
サンプルコレクションの作成
まずは動作確認のため、学生情報(名前・年齢・国名)を格納するコレクションに複数のドキュメントを挿入します。
> db.retrieveDocumentsWithEndsWithParticularCharacterDemo.insertOne({"StudentName":"Adam","StudentAge":25,"StudentCountryName":"LAOS"});
{
"acknowledged" : true,
"insertedId" : ObjectId("5c9c45b32d66697741252456")
}
> db.retrieveDocumentsWithEndsWithParticularCharacterDemo.insertOne({"StudentName":"Sam","StudentAge":24,"StudentCountryName":"ANGOLA"});
{
"acknowledged" : true,
"insertedId" : ObjectId("5c9c45c02d66697741252457")
}
> db.retrieveDocumentsWithEndsWithParticularCharacterDemo.insertOne({"StudentName":"Robert","StudentAge":21,"StudentCountryName":"AUS"});
{
"acknowledged" : true,
"insertedId" : ObjectId("5c9c45cb2d66697741252458")
}
> db.retrieveDocumentsWithEndsWithParticularCharacterDemo.insertOne({"StudentName":"Chris","StudentAge":20,"StudentCountryName":"UK"});
{
"acknowledged" : true,
"insertedId" : ObjectId("5c9c45d92d66697741252459")
}
> db.retrieveDocumentsWithEndsWithParticularCharacterDemo.insertOne({"StudentName":"Larry","StudentAge":23,"StudentCountryName":"US"});
{
"acknowledged" : true,
"insertedId" : ObjectId("5c9c45eb2d6669774125245a")
}
登録済みドキュメントの一覧表示
find()メソッドを使って、コレクション内のすべてのドキュメントを表示して確認しましょう。
> db.retrieveDocumentsWithEndsWithParticularCharacterDemo.find().pretty();
上記クエリを実行すると、次のような出力が得られます。
{
"_id" : ObjectId("5c9c45b32d66697741252456"),
"StudentName" : "Adam",
"StudentAge" : 25,
"StudentCountryName" : "LAOS"
}
{
"_id" : ObjectId("5c9c45c02d66697741252457"),
"StudentName" : "Sam",
"StudentAge" : 24,
"StudentCountryName" : "ANGOLA"
}
{
"_id" : ObjectId("5c9c45cb2d66697741252458"),
"StudentName" : "Robert",
"StudentAge" : 21,
"StudentCountryName" : "AUS"
}
{
"_id" : ObjectId("5c9c45d92d66697741252459"),
"StudentName" : "Chris",
"StudentAge" : 20,
"StudentCountryName" : "UK"
}
{
"_id" : ObjectId("5c9c45eb2d6669774125245a"),
"StudentName" : "Larry",
"StudentAge" : 23,
"StudentCountryName" : "US"
}
「S」で終わる国名を持つドキュメントを検索する
それでは本題です。$regexを使って、StudentCountryNameフィールドの値が文字「S」で終わるドキュメントを取得してみましょう。
> db.retrieveDocumentsWithEndsWithParticularCharacterDemo.find({StudentCountryName: {$regex: "S$"}}).pretty();
実行結果は以下の通りです。「LAOS」「AUS」「US」のように、末尾が「S」で終わる国名を持つドキュメントだけが返されていることが分かります。
{
"_id" : ObjectId("5c9c45b32d66697741252456"),
"StudentName" : "Adam",
"StudentAge" : 25,
"StudentCountryName" : "LAOS"
}
{
"_id" : ObjectId("5c9c45cb2d66697741252458"),
"StudentName" : "Robert",
"StudentAge" : 21,
"StudentCountryName" : "AUS"
}
{
"_id" : ObjectId("5c9c45eb2d6669774125245a"),
"StudentName" : "Larry",
"StudentAge" : 23,
"StudentCountryName" : "US"
}
補足:大文字・小文字を区別せず検索したい場合
$regexはデフォルトで大文字・小文字を区別します。区別せずに検索したい場合は、$optionsに「i」を指定します。
db.collectionName.find({yourFieldName: {$regex: "s$", $options: "i"}}).pretty();
なお、前方不一致の正規表現(末尾一致など)はインデックスを効率的に利用できない場合があるため、大量のデータを扱う際はパフォーマンスへの影響も考慮しておきましょう。
-
MySQLのSELECT文で列の値の末尾に文字を追加する方法
列の値の末尾に文字を追加したい場合は、CONCAT()関数を使って文字列を連結します。この記事では、具体的な手順をサンプルコードとともに解説します。テーブルの作成まず、サンプル用のテーブルを作成しましょう。mysql> create table DemoTable766 (Name varchar(100));Query OK, 0 rows affected (0.65 sec)レコードの挿入insertコマンドを使って、テーブルにいくつかのレコードを挿入します。mysql> insert into DemoTable766 values(John);Query OK, 1 ro
-
Javaを使ってMongoDBコレクションからすべてのドキュメントを取得する方法
MongoDBでは、find()メソッドを使用することで、既存のコレクションからドキュメントを取得できます。この記事では、MongoDBシェルでの基本操作と、Javaプログラムからすべてのドキュメントを取得する手順について詳しく解説します。MongoDBシェルでの構文db.coll.find()各要素の意味は以下のとおりです。db:対象となるデータベースcoll:ドキュメントを取得したいコレクションの名前シェルでの実行例ここでは、MongoDBデータベース内に「students」というコレクションがあり、次のドキュメントが登録されているものとします。{name:"Ram",