정의
구문
$regexMatch
연산자의 구문은 다음과 같습니다.
{ $regexMatch: { input: <expression> , regex: <expression>, options: <expression> } }
필드 | 설명 | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
선택 사항입니다. 다음
|
반환
연산자는 부울을 반환합니다.
true
일치하는 항목이 존재하는 경우false
일치하는 항목이 존재하지 않는 경우
행동
PCRE 라이브러리
버전 6.1부터, MongoDB는 PCRE2(Perl 호환 정규 표현식) 라이브러리를 사용하여 정규 표현식 패턴 일치를 구현합니다. PCRE 2에 대한 자세한 내용은 PCRE 설명서를 참조하세요.
$regexMatch 및 데이터 정렬
$regexMatch
에 대한 문자열 일치는 항상 대소문자를 구분하고 발음 부호를 구분합니다. $regexMatch
은 컬렉션db.collection.aggregate()
및 인덱스(사용된 경우)에 지정된 데이터 정렬을 무시합니다.
예시 를 들어 1
데이터 정렬 강도로 컬렉션 만듭니다.
db.createCollection( "restaurants", { collation: { locale: "fr", strength: 1 } } )
다음 문서를 삽입합니다.
db.restaurants.insertMany( [ { _id: 1, category: "café", status: "Open" }, { _id: 2, category: "cafe", status: "open" }, { _id: 3, category: "cafE", status: "open" } ] )
다음은 컬렉션의 데이터 정렬을 사용하여 대소문자를 구분하지 않고 분음 부호를 구분하지 않는 일치를 수행합니다.
db.restaurants.aggregate( [ { $match: { category: "cafe" } } ] )
[ { _id: 1, category: 'café', status: 'Open' }, { _id: 2, category: 'cafe', status: 'open' }, { _id: 3, category: 'cafE', status: 'open' } ]
그러나 $regexMatch
는 데이터 정렬을 무시합니다. 다음 정규 표현식 패턴 일치 예제는 대소문자를 구분하고 발음 부호를 구분합니다.
db.restaurants.aggregate( [ { $addFields: { resultObject: { $regexMatch: { input: "$category", regex: /cafe/ } } } } ] ) db.restaurants.aggregate( [ { $addFields: { resultObject: { $regexMatch: { input: "$category", regex: /cafe/ } } } } ], { collation: { locale: "fr", strength: 1 } } // Ignored in the $regexMatch )
두 연산 모두 다음과 같은 결과를 반환합니다.
{ "_id" : 1, "category" : "café", "resultObject" : null } { "_id" : 2, "category" : "cafe", "resultObject" : { "match" : "cafe", "idx" : 0, "captures" : [ ] } } { "_id" : 3, "category" : "cafE", "resultObject" : null }
쿼리 데이터 정렬을 무시하므로 category
문자열에서 정확히 일치해야 하며(대소문자 및 악센트 표시 포함), 이는 문서 _id: 2
만 일치됨을 의미합니다.
대소문자를 구분하지 않는 정규식 패턴 일치를 수행하려면 i
옵션을 대신 사용하세요. i
옵션을 예시로 참조하세요.
예시
$regexMatch
및 해당 옵션
이 예시에서 말한 $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 }
i
옵션
참고
regex
및 options
필드 모두에 옵션을 지정할 수 없습니다.
대소문자를 구분하지 않는 패턴 일치를 수행하려면 i 옵션을 regex 필드의 일부로 포함하거나 options 필드에 포함합니다.
// 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" } }
예를 들어, 다음 집계는 description
필드에서 대소문자를 구분하지 않는 $regexMatch
를 수행합니다. 정규식 패턴 /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 }
m
옵션
참고
regex
및 options
필드 모두에 옵션을 지정할 수 없습니다.
지정된 앵커와 일치시키려면(예: ^
, $
) 여러 줄 문자열의 각 줄에 대해 m 옵션을 regex 필드의 일부로 포함하거나 options 필드에 포함해야 합니다.
// 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" } }
다음 예제에는 여러 줄 문자열의 경우 문자 s
또는 S
로 시작하는 줄을 일치시키는 i
및 m
옵션이 모두 포함되어 있습니다.
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 }
x
옵션
참고
regex
및 options
필드 모두에 옵션을 지정할 수 없습니다.
패턴에서 이스케이프되지 않은 모든 공백 문자와 주석(이스케이프되지 않은 해시 #
문자와 다음 줄 바꿈 문자로 표시됨)을 무시하려면 옵션 필드에 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 }
s
옵션
참고
regex
및 options
필드 모두에 옵션을 지정할 수 없습니다.
패턴의 점 문자(예: .
)가 새 줄 문자를 포함한 모든 문자와 일치하도록 하려면 options 필드에 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 }
$regexMatch
를 사용해 이메일 주소를 확인합니다.
다음 문서를 사용하여 샘플 collection feedback
을 만듭니다.
db.feedback.insertMany([ { "_id" : 1, comment: "Hi, I'm just reading about MongoDB -- [email protected]" }, { "_id" : 2, comment: "I wanted to concatenate a string" }, { "_id" : 3, comment: "How do I convert a date to string? Contact me at either [email protected] or [email protected]" }, { "_id" : 4, comment: "It's just me. I'm testing. [email protected]" } ])
다음 집계는 $regexMatch
를 사용하여 comment
필드에 @mongodb.com
을 포함하는 이메일 주소가 있는지 확인하고 피드백을 Employee
또는 External
로 분류합니다.
db.feedback.aggregate( [ { $addFields: { "category": { $cond: { if: { $regexMatch: { input: "$comment", regex: /[a-z0-9_.+-][email protected]/i } }, then: "Employee", else: "External" } } } },
이 작업은 다음 문서를 반환합니다.
{ "_id" : 1, "comment" : "Hi, I'm just reading about MongoDB -- [email protected]", "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 [email protected] or [email protected]", "category" : "Employee" } { "_id" : 4, "comment" : "It's just me. I'm testing. [email protected]", "category" : "Employee" }