적용 대상:
MongoDB vCore
연산자는 $regex
쿼리에서 패턴 일치에 대한 정규식 기능을 제공합니다. 필드가 지정된 정규식 패턴과 일치하는 문서를 검색할 수 있습니다. 이 연산자는 유연한 문자열 검색, 패턴 유효성 검사 및 복잡한 텍스트 필터링 작업에 유용합니다.
문법
연산자의 $objectToArray
구문은 다음과 같습니다.
{
<field>: { $regex: <pattern>, $options: <options> }
}
또는 더 짧은 형식:
{
<field>: { $regex: /<pattern>/<options> }
}
매개 변수
설명 | |
---|---|
<field> |
검색할 필드입니다. 문자열 값을 포함해야 합니다. |
<pattern> |
일치시킬 정규식 패턴입니다. |
<options> |
선택 사항입니다. 대/소문자를 구분하지 않는 일치와 같은 정규식 옵션입니다. 일반적인 옵션으로는 'i'(대/소문자를 구분하지 않는 경우), 'm'(다중선), 's'(점 모두) 및 'x'(확장됨)가 있습니다. |
예시
예제 1: 이름 패턴별 저장소 찾기
이 예제에서는 이름에 "Consultants"가 포함된 모든 저장소를 찾습니다(대/소문자를 구분하지 않음).
db.stores.find(
{ "name": { $regex: "Consultants", $options: "i" } },
{ "_id": 1, "name": 1, "storeOpeningDate": 1 }
).limit(3)
쿼리는 사례에 관계없이 "컨설턴트"가 포함된 매장 이름과 일치를 반환하여 컨설팅 회사에 속한 매장을 식별하는 데 도움을 줍니다.
{
"_id": "39acb3aa-f350-41cb-9279-9e34c004415a",
"name": "First Up Consultants | Bed and Bath Pantry - Port Antone",
"storeOpeningDate": "2024-09-19T17:31:59.665Z"
},
{
"_id": "26afb024-53c7-4e94-988c-5eede72277d5",
"name": "First Up Consultants | Microphone Bazaar - South Lexusland",
"storeOpeningDate": "2024-09-10T13:43:51.209Z"
},
{
"_id": "62438f5f-0c56-4a21-8c6c-6bfa479494ad",
"name": "First Up Consultants | Plumbing Supply Shoppe - New Ubaldofort",
"storeOpeningDate": "2024-09-19T08:27:44.268Z"
}
예제 2: 범주 이름에 대한 고급 패턴 일치
이 예제에서는 모음으로 시작하는 범주 이름으로 제품을 판매하는 상점을 찾습니다.
db.stores.find(
{
"sales.salesByCategory.categoryName": { $regex: "^[AEIOUaeiou]", $options: "" }
},
{ "_id": 1, "name": 1, "sales.salesByCategory.categoryName": 1}
).limit(2)
쿼리는 캐럿(^) 앵커 및 문자 클래스를 사용하여 모음으로 시작하는 범주 이름과 일치합니다.
{
"_id": "e6410bb3-843d-4fa6-8c70-7472925f6d0a",
"name": "Relecloud | Toy Collection - North Jaylan",
"sales": {
"salesByCategory": [
{ "categoryName": "Educational Toys" }
]
}
},
{
"_id": "4e064f0a-7e30-4701-9a80-eff3caf46ce8",
"name": "Fourth Coffee | Outdoor Furniture Deals - Lucianohaven",
"sales": {
"salesByCategory": [
{ "categoryName": "Outdoor Swings" },
{ "categoryName": "Hammocks" }
]
}
}
예제 3: 특정 명명 규칙이 있는 저장소 찾기
이 예제에서는 파이프 문자(|) 구분 기호가 포함된 이름을 가진 저장소를 찾습니다.
db.stores.find(
{ "name": { $regex: "\\|" }},
{ "_id": 1, "name": 1, "sales.salesByCategory.categoryName": 1}).limit(2)
쿼리는 파이프 문자가 포함된 이름을 가진 저장소를 검색합니다. 이는 데이터 세트의 명명 규칙으로 보입니다.
{
"_id": "905d1939-e03a-413e-a9c4-221f74055aac",
"name": "Trey Research | Home Office Depot - Lake Freeda",
"sales": {
"salesByCategory": [
{ "categoryName": "Desk Lamps" }
]
}
},
{
"_id": "a715ab0f-4c6e-4e9d-a812-f2fab11ce0b6",
"name": "Lakeshore Retail | Holiday Supply Hub - Marvinfort",
"sales": {
"salesByCategory": [
{ "categoryName": "Stockings" }
]
}
}
예제 4: 할인 범주에 대한 복잡한 패턴
이 예제에서는 "Bath"와 "s"로 끝나는 범주가 모두 포함된 저장소를 찾습니다.
db.stores.aggregate([
{ $match: { "promotionEvents.discounts.categoryName": { $regex: "Bath.*s$", $options: "i" } } },
{ $project: { "_id": 1, "name": 1, "promotionEvents.discounts.categoryName":1 }},
{ $match: {"promotionEvents.discounts.categoryName": { $ne: [] }} },
{ $limit: 2 }])
쿼리는 리터럴 텍스트 일치, 점 별 수량자(.*) 및 문자열 끝 앵커($)와 같은 여러 정규식 기능을 결합합니다.
{
"_id": "39acb3aa-f350-41cb-9279-9e34c004415a",
"name": "First Up Consultants | Bed and Bath Pantry - Port Antone",
"promotionEvents": [
{
"discounts": [
{ "categoryName": "Bath Sheets", "discountPercentage": 16 },
{ "categoryName": "Bath Accessories", "discountPercentage": 11 }
]
},
{
"discounts": [
{ "categoryName": "Bath Mats", "discountPercentage": 22 }
]
},
{
"discounts": [
{ "categoryName": "Bath Towels", "discountPercentage": 21 },
{ "categoryName": "Bathrobes", "discountPercentage": 19 },
{ "categoryName": "Bath Accessories", "discountPercentage": 5 }
]
},
{
"discounts": [
{ "categoryName": "Bath Sheets", "discountPercentage": 25 },
{ "categoryName": "Bath Towels", "discountPercentage": 15 }
]
}
]
},
{
"_id": "27ef6004-70fa-4217-8395-0eabc4cc9841",
"name": "Fabrikam, Inc. | Bed and Bath Store - O'Connerborough",
"promotionEvents": [
{
"discounts": [
{ "categoryName": "Bath Accessories", "discountPercentage": 24 }
]
},
{
"discounts": [
{ "categoryName": "Bathrobes", "discountPercentage": 18 },
{ "categoryName": "Bath Towels", "discountPercentage": 14 },
{ "categoryName": "Bath Accessories", "discountPercentage": 20 }
]
}
]
}
관련 콘텐츠
- MongoDB에서 Azure Cosmos DB for MongoDB(vCore)로 마이그레이션하기 위한 옵션 검토
- MongoDB와의 기능 호환성에 대해 자세히 알아보기