Docs Menu
Docs Home
/
MongoDB マニュアル
/ / /

$regexMatch(集計)

項目一覧

  • 定義
  • 構文
  • 動作
$regexMatch

正規表現(regex)によるパターン マッチングを行い、次のように結果を返します。

  • true : 一致するものが存在する場合

  • false : 一致するものが存在しない場合

$regexMatch演算子の構文は次のとおりです。

{ $regexMatch: { input: <expression> , regex: <expression>, options: <expression> } }
フィールド
説明

文字列 に正規表現パターンを適用します。文字列、または文字列に変換される有効な を指定できます。

適用する正規表現パターン。 または正規表現パターン のいずれかに解決される任意の有効な を指定できます。string/<pattern>/正規表現/<pattern>/を使用する場合、正規表現オプションimを指定することもできます(ただし、 sまたはxオプションは含まれません)。

  • "pattern"

  • /<pattern>/

  • /<pattern>/<options>

あるいは、オプションフィールドを使用して正規表現オプションを指定することもできます。 sまたはxオプションを指定するには、オプションフィールドを使用する必要があります。

regexoptionsフィールドと フィールドの両方にオプションを指定することはできません。

任意。次の <options> は正規表現で使用できます。

注意

regexoptionsフィールドと フィールドの両方にオプションを指定することはできません。

オプション
説明
i
大文字と小文字の両方で一致するように、大文字と小文字を区別せずに一致します。 オプションは、 optionsフィールドまたは 正規表現 フィールドの一部として指定できます。
m

アンカーを含むパターン(先頭は ^、末尾は $)の場合、複数行の値を含む文字列に対して各行の先頭または末尾で一致します。このオプションを指定しない場合、これらのアンカーは文字列の先頭か末尾で一致します。

パターンにアンカーが含まれていない場合、または文字列値に改行文字が含まれていない場合(\n など)、 m オプションは効果がありません。

x

エスケープされたり、文字クラスに含まれていたりしない限り、 パターン内のすべての空白文字を無視できる「拡張」機能です。

さらに、エスケープされていないハッシュまたはパウンド(#)記号と次の改行を含む間にある文字は無視されるため、複雑なパターンでコメントを含めることができます。これはデータ文字にのみ適用され、パターン内の特殊文字シーケンスに空白文字が表示されることはありません。

x オプションは VT 文字の処理には影響しません(コード 11)。

オプションはoptionsフィールドでのみ指定できます。

s

ドット記号(.)を、改行文字を含むすべての文字と一致します。

オプションはoptionsフィールドでのみ指定できます。

この演算子は、次のようにブール値を返します。

  • true : 一致するものが存在する場合

  • false : 一致するものが存在しない場合

Tip

以下も参照してください。

バージョン 6.1 以降、MongoDB では、正規表現パターン マッチングを実装するために PCRE 2(Perl Compatible Regular Expressions、Perl 互換正規表現)ライブラリが使用されます。PCRE2 の詳細については、PCRE ドキュメントを参照してください。

$regexMatchは、コレクションに指定された照合、 db.collection.aggregate() 、およびインデックス(使用されている場合)を無視します。

たとえば、照合強度が1のサンプル コレクションを作成するとします(つまり、 基本文字のみを比較し、大文字と小文字や発音区別符号などの他の違いは無視します)。

db.createCollection( "myColl", { collation: { locale: "fr", strength: 1 } } )

次のドキュメントを挿入します。

db.myColl.insertMany([
{ _id: 1, category: "café" },
{ _id: 2, category: "cafe" },
{ _id: 3, category: "cafE" }
])

次の操作では、 コレクションの照合順序を使用して、大文字と小文字を区別せず、発音区別符号も区別しない一致が実行されます。

db.myColl.aggregate( [ { $match: { category: "cafe" } } ] )

この操作では、次の 3 つのドキュメントが返されます。

{ "_id" : 1, "category" : "café" }
{ "_id" : 2, "category" : "cafe" }
{ "_id" : 3, "category" : "cafE" }

ただし、集計式$regexMatchは照合を無視します。つまり、次の正規表現パターン一致の例では、大文字と小文字が区別され、発音区別符号も区別されます。

db.myColl.aggregate( [ { $addFields: { results: { $regexMatch: { input: "$category", regex: /cafe/ } } } } ] )
db.myColl.aggregate(
[ { $addFields: { results: { $regexMatch: { input: "$category", regex: /cafe/ } } } } ],
{ collation: { locale: "fr", strength: 1 } } // Ignored in the $regexMatch
)

どちらの操作も以下を返します。

{ "_id" : 1, "category" : "café", "results" : false }
{ "_id" : 2, "category" : "cafe", "results" : true }
{ "_id" : 3, "category" : "cafE", "results" : false }

大文字と小文字を区別しない正規表現パターン マッチングを実行するには、代わりにiオプションを使用します。 例については、 iオプションを参照してください。

この例で説明されている$regexMatch演算子の動作を示すには、次のドキュメントを含むサンプル コレクションproductsを作成します。

db.products.insertMany([
{ _id: 1, description: "Single LINE description." },
{ _id: 2, description: "First lines\nsecond line" },
{ _id: 3, description: "Many spaces before line" },
{ _id: 4, description: "Multiple\nline descriptions" },
{ _id: 5, description: "anchors, links and hyperlinks" },
{ _id: 6, description: "métier work vocation" }
])

デフォルトでは、 $regexMatchは大文字と小文字を区別する一致を実行します。 たとえば、次の集計ではdescriptionフィールドで大文字と小文字を区別する$regexMatchが実行されます。 正規表現パターン/line/ではグループは指定されていません。

db.products.aggregate([
{ $addFields: { result: { $regexMatch: { input: "$description", regex: /line/ } } } }
])

この操作では、以下を返します。

{ "_id" : 1, "description" : "Single LINE description.", "result" : false }
{ "_id" : 2, "description" : "First lines\nsecond line", "result" : true }
{ "_id" : 3, "description" : "Many spaces before line", "result" : true }
{ "_id" : 4, "description" : "Multiple\nline descriptions", "result" : true }
{ "_id" : 5, "description" : "anchors, links and hyperlinks", "result" : false }
{ "_id" : 6, "description" : "métier work vocation", "result" : false }

次の正規表現パターン/lin(e|k)/は、パターン内にグループ化(e|k)を指定します。

db.products.aggregate([
{ $addFields: { result: { $regexMatch: { input: "$description", regex: /lin(e|k)/ } } } }
])

この操作では、以下を返します。

{ "_id" : 1, "description" : "Single LINE description.", "result" : false }
{ "_id" : 2, "description" : "First lines\nsecond line", "result" : true }
{ "_id" : 3, "description" : "Many spaces before line", "result" : true }
{ "_id" : 4, "description" : "Multiple\nline descriptions", "result" : true }
{ "_id" : 5, "description" : "anchors, links and hyperlinks", "result" : true }
{ "_id" : 6, "description" : "métier work vocation", "result" : false }

注意

regexoptionsフィールドと フィールドの両方にオプションを指定することはできません。

大文字と 小文字 を区別しないパターン一致を実行するには、 正規表現 フィールドの一部または オプション フィールドに i オプションを含めます。

// Specify i as part of the regex field
{ $regexMatch: { input: "$description", regex: /line/i } }
// Specify i in the options field
{ $regexMatch: { input: "$description", regex: /line/, options: "i" } }
{ $regexMatch: { input: "$description", regex: "line", options: "i" } }

For example, the following aggregation performs a case-insensitive $regexMatch on the description field. 正規表現パターン/line/ではグループは指定されていません。

db.products.aggregate([
{ $addFields: { result: { $regexMatch: { input: "$description", regex: /line/i } } } }
])

この操作により、次のドキュメントが返されます。

{ "_id" : 1, "description" : "Single LINE description.", "result" : true }
{ "_id" : 2, "description" : "First lines\nsecond line", "result" : true }
{ "_id" : 3, "description" : "Many spaces before line", "result" : true }
{ "_id" : 4, "description" : "Multiple\nline descriptions", "result" : true }
{ "_id" : 5, "description" : "anchors, links and hyperlinks", "result" : false }
{ "_id" : 6, "description" : "métier work vocation", "result" : false }

注意

regexoptionsフィールドと フィールドの両方にオプションを指定することはできません。

指定されたアンカー(例: ^$ )の各行には、string 正規表現 フィールドの一部または オプション フィールドに m オプションを含め 。

// Specify m as part of the regex field
{ $regexMatch: { input: "$description", regex: /line/m } }
// Specify m in the options field
{ $regexMatch: { input: "$description", regex: /line/, options: "m" } }
{ $regexMatch: { input: "$description", regex: "line", options: "m" } }

次の例には、複数行の文字列に対して文字i mまたは で始まる行を検索するために、s オプションとS オプションの両方が含まれています。

db.products.aggregate([
{ $addFields: { result: { $regexMatch: { input: "$description", regex: /^s/im } } } }
])

この操作では、以下を返します。

{ "_id" : 1, "description" : "Single LINE description.", "result" : true }
{ "_id" : 2, "description" : "First lines\nsecond line", "result" : true }
{ "_id" : 3, "description" : "Many spaces before line", "result" : false }
{ "_id" : 4, "description" : "Multiple\nline descriptions", "result" : false }
{ "_id" : 5, "description" : "anchors, links and hyperlinks", "result" : false }
{ "_id" : 6, "description" : "métier work vocation", "result" : false }

注意

regexoptionsフィールドと フィールドの両方にオプションを指定することはできません。

パターン内のエスケープされていない空白文字とコメント(エスケープされていないハッシュ「# 」文字と次の改行文字によって示される)を無視するには、 オプション フィールドに s オプションを含めます。

// Specify x in the options field
{ $regexMatch: { input: "$description", regex: /line/, options: "x" } }
{ $regexMatch: { input: "$description", regex: "line", options: "x" } }

次の例には、エスケープされていない空白とコメントをスキップするためのxオプションが含まれています。

db.products.aggregate([
{ $addFields: { returns: { $regexMatch: { input: "$description", regex: /lin(e|k) # matches line or link/, options:"x" } } } }
])

この操作では、以下を返します。

{ "_id" : 1, "description" : "Single LINE description.", "returns" : false }
{ "_id" : 2, "description" : "First lines\nsecond line", "returns" : true }
{ "_id" : 3, "description" : "Many spaces before line", "returns" : true }
{ "_id" : 4, "description" : "Multiple\nline descriptions", "returns" : true }
{ "_id" : 5, "description" : "anchors, links and hyperlinks", "returns" : true }
{ "_id" : 6, "description" : "métier work vocation", "returns" : false }

注意

regexoptionsフィールドと フィールドの両方にオプションを指定することはできません。

ドット記号( .)が改行文字 を含む すべての文字と一致するようにするには、 オプション フィールドに s オプションを含めます。

// Specify s in the options field
{ $regexMatch: { input: "$description", regex: /m.*line/, options: "s" } }
{ $regexMatch: { input: "$description", regex: "m.*line", options: "s" } }

次の例には、ドット文字()が改行を 含む すべての文字と一致するようにするためのsオプションと、大文字と小文字を区別しない一致を実行するためのiオプションが含まれています。

db.products.aggregate([
{ $addFields: { returns: { $regexMatch: { input: "$description", regex:/m.*line/, options: "si" } } } }
])

この操作では、以下を返します。

{ "_id" : 1, "description" : "Single LINE description.", "returns" : false }
{ "_id" : 2, "description" : "First lines\nsecond line", "returns" : false }
{ "_id" : 3, "description" : "Many spaces before line", "returns" : true }
{ "_id" : 4, "description" : "Multiple\nline descriptions", "returns" : true }
{ "_id" : 5, "description" : "anchors, links and hyperlinks", "returns" : false }
{ "_id" : 6, "description" : "métier work vocation", "returns" : false }

次のドキュメントを含む feedback のサンプル コレクションを作成します。

db.feedback.insertMany([
{ "_id" : 1, comment: "Hi, I'm just reading about MongoDB -- aunt.arc.tica@example.com" },
{ "_id" : 2, comment: "I wanted to concatenate a string" },
{ "_id" : 3, comment: "How do I convert a date to string? Contact me at either cam@mongodb.com or c.dia@mongodb.com" },
{ "_id" : 4, comment: "It's just me. I'm testing. fred@MongoDB.com" }
])

次の集計では、 $regexMatchを使用して、 commentフィールドに@mongodb.comのメールアドレスが含まれているかどうかを確認し、フィードバックをEmployeeまたはExternalに分類します。

db.feedback.aggregate( [
{ $addFields: {
"category": { $cond: { if: { $regexMatch: { input: "$comment", regex: /[a-z0-9_.+-]+@mongodb.com/i } },
then: "Employee",
else: "External" } }
} },

この操作により、次のドキュメントが返されます。

{ "_id" : 1, "comment" : "Hi, I'm just reading about MongoDB -- aunt.arc.tica@example.com", "category" : "External" }
{ "_id" : 2, "comment" : "I wanted to concatenate a string", "category" : "External" }
{ "_id" : 3, "comment" : "How do I convert a date to string? Contact me at either cam@mongodb.com or c.dia@mongodb.com", "category" : "Employee" }
{ "_id" : 4, "comment" : "It's just me. I'm testing. fred@MongoDB.com", "category" : "Employee" }

戻る

$regexFindAll