注意
このページでは、自己管理型(Atlas Search 以外)配置に対する正規表現検索機能について説明します。MongoDB Atlas でホストされているデータに対して、MongoDB では、改良された全文検索ソリューションである Atlas Search を提供します。Atlas Search は独自の $regex演算子を持ちます。詳細については、Atlas Search ドキュメントの $regex を参照してください。
定義
互換性
次の環境でホストされる配置には $regex を使用できます。
MongoDB Atlas はクラウドでの MongoDB 配置のためのフルマネージド サービスです
MongoDB Enterprise: サブスクリプションベースの自己管理型 MongoDB バージョン
MongoDB Community: ソースが利用可能で、無料で使用できる自己管理型の MongoDB のバージョン
構文
Use one of the following syntax variations:
{ <field>: { $regex: /pattern/, $options: '<options>' } } { "<field>": { "$regex": "pattern", "$options": "<options>" } } { <field>: { $regex: /pattern/<options> } }
注意
To use $regex with mongodump, enclose the query document in single quotes ('{ ... }') to ensure it doesn't interact with the shell.
クエリ ドキュメントは、フィールド名と演算子を引用符で囲むことを含む、拡張 JSON V 2 形式(緩和モード、標準モード、厳密モードのいずれか)である必要があります。以下がその例です。
mongodump -d=sample_mflix -c=movies -q='{"year": {"$regex": "20"}}'
You can also use regular expression objects (/pattern/) to specify regular expressions:
{ <field>: /pattern/<options> }
For restrictions on syntax use, see $regex vs. /pattern/ Syntax.
The following <options> are available for regular expressions:
オプション | 説明 |
|---|---|
| 大文字と小文字を区別せずに一致します。 例、「 大文字と小文字を区別しない正規表現一致の実行 」を参照してください。 |
| For patterns that include anchors ( If the pattern has no anchors or if the string value has no newline characters (for example, |
| エスケープされたり、文字クラスに含まれていたりしない限り、 さらに、エスケープされていないハッシュまたはパウンド( The |
| Allows the dot character ( |
| Unicode option, which is accepted but redundant. UTF is enabled by default for |
注意
$regex doesn't support the global search modifier g.
動作
$regex と /pattern/ の構文
$in 式
$inクエリ述語演算子に正規表現を含めるには、JavaScript 正規表現オブジェクトのみを使用できます( /pattern/ )。
以下に例を挙げます。
{ name: { $in: [ /^acme/i, /^ack/ ] } }
$in 演算子内では $regex 演算子式を使用できません。
フィールドの暗黙的な AND 条件
フィールドのクエリ条件をカンマで区切ったリストに正規表現を含めるには、$regex 演算子を使用します。例は次のとおりです。
{ name: { $regex: /acme.*corp/i, $nin: [ 'acmeblahcorp' ] } } { name: { $regex: /acme.*corp/, $options: 'i', $nin: [ 'acmeblahcorp' ] } } { name: { $regex: 'acme.*corp', $options: 'i', $nin: [ 'acmeblahcorp' ] } }
x と s オプション
x オプションまたは s オプションのいずれかを使用するには、$regex 演算子とともに $options 演算子式を使用する必要があります。たとえば、 i オプションと s オプションを指定するには、次のように両方とも $options を使用する必要があります。
{ name: { $regex: /acme.*corp/, $options: "si" } } { name: { $regex: 'acme.*corp', $options: "si" } }
PCRE と JavaScript
To use PCRE-supported features in a regular expression that aren't supported in JavaScript, use the $regex operator and specify the pattern as a string.
大文字と小文字を区別しない文字列を一致させるには次のとおり。
"(?i)"は、大文字と小文字を区別しない一致を開始します。"(?-i)"は、大文字と小文字を区別しない一致を終了します。
たとえば、正規表現"(?i)a(?-i)cme" は、次の文字列と一致します。
"a"または"A"ではじまる文字列。これは、大文字と小文字を区別しない一致です。"cme"で終了する文字列。これは大文字と小文字を区別する一致です。
以下の文字列は、例の正規表現と一致します。
"acme""Acme"
次の例では、$regex 演算子を使用して、正規表現 "(?i)a(?-i)cme" と一致する name フィールドの文字列を検索します。
{ name: { $regex: "(?i)a(?-i)cme" } }
バージョン 6.1 以降、MongoDB では、正規表現パターン マッチングを実装するために PCRE 2(Perl Compatible Regular Expressions、Perl 互換正規表現)ライブラリが使用されます。Perl 互換正規表現 (PCRE2) の詳細については、 PCRE ドキュメントを参照してください。
$regex および $not
The $not operator can perform a logical NOT operation on both:
Regular expression objects (
/pattern/)以下に例を挙げます。
db.inventory.find( { item: { $not: /^p.*/ } } ) $regex演算子式以下に例を挙げます。
db.inventory.find( { item: { $not: { $regex: "^p.*" } } } ) db.inventory.find( { item: { $not: { $regex: /^p.*/ } } } )
インデックスの使用
Index use and performance for $regex queries depends on whether the query is case-sensitive or case-insensitive.
大文字と小文字を区別するクエリ
For case sensitive regular expression queries, if an index exists for the field, then MongoDB matches the regular expression for the values in the index. This can be faster than a collection scan.
Further optimization can occur if the regular expression is a "prefix expression", which means that all potential matches start with the same string. This allows MongoDB to construct a "range" from that prefix and only match values from the index within the specified range.
A regular expression is a "prefix expression" if it starts with a caret (^) or a left anchor (\A), followed by a string of simple symbols. For example, the regex /^abc.*/ is optimized to match only the values from the index that start with abc.
Additionally, although /^a/, /^a.*/, and /^a.*$/ match equivalent strings, they have different performance characteristics. All of these expressions use an index if an appropriate index exists. However, /^a.*/, and /^a.*$/ are slower. /^a/ can stop scanning after matching the prefix.
大文字と小文字を区別しないクエリ
大文字と小文字を区別しないインデックスは、$regex クエリのパフォーマンスを向上させません。$regex演算子は照合順序を考慮しないため、そのようなインデックスを活用できません。
例
このセクションの例では、次の products コレクションを使用します。
db.products.insertMany( [ { _id: 100, sku: "abc123", description: "Single line description." }, { _id: 101, sku: "abc789", description: "First line\nSecond line" }, { _id: 102, sku: "xyz456", description: "Many spaces before line" }, { _id: 103, sku: "xyz789", description: "Multiple\nline description" }, { _id: 104, sku: "Abc789", description: "SKU starts with A" } ] )
LIKE 一致の実行
次の例は、 sku フィールドが "%789" のようになっているすべてのドキュメントと一致します。
db.products.find( { sku: { $regex: /789$/ } } )
The example is similar to the following SQL LIKE statement:
SELECT * FROM products WHERE sku like "%789";
出力例:
[ { _id: 101, sku: 'abc789', description: 'First line\nSecond line' }, { _id: 103, sku: 'xyz789', description: 'Multiple\nline description' }, { _id: 104, sku: 'Abc789', description: 'SKU starts with A' } ]
大文字と小文字を区別しない正規表現一致の実行
The following example uses the i option to perform a case-insensitive match for sku values that start with ABC:
db.products.find( { sku: { $regex: /^ABC/i } } )
出力例:
[ { _id: 100, sku: 'abc123', description: 'Single line description.' }, { _id: 101, sku: 'abc789', description: 'First line\nSecond line' }, { _id: 104, sku: 'Abc789', description: 'SKU starts with A' } ]
Match Whole Words Only
Use the \b word boundary anchor to match whole words only. A word boundary matches the position between a word character and a non-word character, or at the start or end of a string.
The following example matches documents where the description field contains the word line as a complete word, but not as part of another word like multiline:
db.products.find( { description: { $regex: /\bline\b/ } } )
[ { _id: 100, sku: 'abc123', description: 'Single line description.' }, { _id: 101, sku: 'abc789', description: 'First line\nSecond line' }, { _id: 102, sku: 'xyz456', description: 'Many spaces before line' } ]
MongoDB does not return the document with _id: 103 because its description field contains line only as part of the word Multiple\nline. The \n (newline) acts as a word boundary for the second occurrence.
注意
For matching word boundaries with UTF-8 characters, see Extend Regex Options to Match Characters Outside of ASCII.
指定したパターンで始まる行の複数行一致
次の例では、 m オプションを使用して、複数行の文字列に対して文字 S で始まる行と一致させます。
db.products.find( { description: { $regex: /^S/, $options: 'm' } } )
出力例:
[ { _id: 100, sku: 'abc123', description: 'Single line description.' }, { _id: 101, sku: 'abc789', description: 'First line\nSecond line' }, { _id: 104, sku: 'Abc789', description: 'SKU starts with A' } ]
m オプションを指定しない場合、出力例は次のようになります。
[ { _id: 100, sku: 'abc123', description: 'Single line description.' }, { _id: 104, sku: 'Abc789', description: 'SKU starts with A' } ]
If the $regex pattern doesn't contain an anchor, the pattern matches against the whole string. For example:
db.products.find( { description: { $regex: /S/ } } )
出力例:
[ { _id: 100, sku: 'abc123', description: 'Single line description.' }, { _id: 101, sku: 'abc789', description: 'First line\nSecond line' }, { _id: 104, sku: 'Abc789', description: 'SKU starts with A' } ]
改行に合わせるには、.ドット記号を使用する
The following example uses the s option to allow the dot character (.) to match all characters including new line, and the i option to perform a case-insensitive match:
db.products.find( { description: { $regex: /m.*line/, $options: 'si' } } )
出力例:
[ { _id: 102, sku: 'xyz456', description: 'Many spaces before line' }, { _id: 103, sku: 'xyz789', description: 'Multiple\nline description' } ]
Without the s option, the query returns:
[ { _id: 102, sku: 'xyz456', description: 'Many spaces before line' } ]
パターン内の空白を無視する
次の例では、x オプションを使用して、一致するパターン内で # で始まり、\n で終わる空白とコメントは無視します。
var pattern = "abc #category code\n123 #item number" db.products.find( { sku: { $regex: pattern, $options: "x" } } )
出力例:
[ { _id: 100, sku: 'abc123', description: 'Single line description.' } ]
文字列内の大文字と小文字を一致させるための正規表現の使用
次の例では、正規表現 "(?i)a(?-i)bc" を使用して、次の内容を含む sku フィールド文字列と一致させます。
"abc""Abc"
db.products.find( { sku: { $regex: "(?i)a(?-i)bc" } } )
出力例:
[ { _id: 100, sku: 'abc123', description: 'Single line description.' }, { _id: 101, sku: 'abc789', description: 'First line\nSecond line' }, { _id: 104, sku: 'Abc789', description: 'SKU starts with A' } ]
正規表現オプション拡張による ASCII 以外の文字との一致
バージョン 6.1 で追加。
By default, some regex options (such as /b and /w) only recognize ASCII characters. This can cause unexpected results when performing regex matches against UTF-8 characters.
MongoDB 6.1 以降では、UTF-8 文字と一致させるために*UCP 正規表現オプションを指定できます。
重要
UCP オプションのパフォーマンス
*UCP オプションを使用すると、*UCP で一致を実行するためには複数のステージのテーブルルックアップが必要となるため、オプションが指定されていないクエリよりも遅くなります。
たとえば、 songs コレクション内の次のドキュメントを考えてみましょう。
db.songs.insertMany( [ { _id: 0, "artist" : "Blue Öyster Cult", "title": "The Reaper" }, { _id: 1, "artist": "Blue Öyster Cult", "title": "Godzilla" }, { _id: 2, "artist" : "Blue Oyster Cult", "title": "Take Me Away" } ] )
次の正規表現クエリは、\b オプションを使用して正規表現の一致を行います。\b オプションは、単語の境界に一致します。
db.songs.find( { artist: { $regex: /\byster/ } } )
出力例:
[ { _id: 0, artist: 'Blue Öyster Cult', title: 'The Reaper' }, { _id: 1, artist: 'Blue Öyster Cult', title: 'Godzilla' } ]
The previous results are unexpected because none of the whole words in the returned artist fields begin with the matched string (yster). The Ö character in documents _id: 0 and _id: 1 is ignored when performing the match because Ö is a UTF-8 character.
The expected result is the query doesn't return any documents.
クエリが UTF-8 文字を認識できるようにするには、次のようにパターンの前に *UCP オプションを指定します。
db.songs.find( { artist: { $regex: "(*UCP)/\byster/" } } )
The previous query doesn't return any documents, which is the expected result because none of the whole words in the artist fields begin with the string yster.
Tip
正規表現パターンのエスケープ文字
When specifying *UCP or any other regular expression option, use the correct escape characters for your shell or driver.