正则表达式

$regex 运算符用于通过正则表达式进行模式匹配,尤其适用于查询字符串字段以寻找符合特定模式的匹配项。 常见用例包括搜索字段包含子字符串、以特定前缀开头或匹配复杂模式的文档。

语法

{ "field": { $regex: /pattern/, $options: '<options>' }  }

参数

参数 DESCRIPTION
field 要查询的文档中的字段
/pattern/ 要匹配的正则表达式模式
options 用于修改正则表达式行为的可选标志。 常见选项包括 i,用于不区分大小写的匹配,m 用于多行匹配,等等。

例子

请查看 StoreData 数据库中 stores 集合的这个示例文档。

{
    "_id": "0fcc0bf0-ed18-4ab8-b558-9848e18058f4",
    "name": "First Up Consultants | Beverage Shop - Satterfieldmouth",
    "location": {
        "lat": -89.2384,
        "lon": -46.4012
    },
    "staff": {
        "totalStaff": {
            "fullTime": 8,
            "partTime": 20
        }
    },
    "sales": {
        "totalSales": 75670,
        "salesByCategory": [
            {
                "categoryName": "Wine Accessories",
                "totalSales": 34440
            },
            {
                "categoryName": "Bitters",
                "totalSales": 39496
            },
            {
                "categoryName": "Rum",
                "totalSales": 1734
            }
        ]
    },
    "promotionEvents": [
        {
            "eventName": "Unbeatable Bargain Bash",
            "promotionalDates": {
                "startDate": {
                    "Year": 2024,
                    "Month": 6,
                    "Day": 23
                },
                "endDate": {
                    "Year": 2024,
                    "Month": 7,
                    "Day": 2
                }
            },
            "discounts": [
                {
                    "categoryName": "Whiskey",
                    "discountPercentage": 7
                },
                {
                    "categoryName": "Bitters",
                    "discountPercentage": 15
                },
                {
                    "categoryName": "Brandy",
                    "discountPercentage": 8
                },
                {
                    "categoryName": "Sports Drinks",
                    "discountPercentage": 22
                },
                {
                    "categoryName": "Vodka",
                    "discountPercentage": 19
                }
            ]
        },
        {
            "eventName": "Steal of a Deal Days",
            "promotionalDates": {
                "startDate": {
                    "Year": 2024,
                    "Month": 9,
                    "Day": 21
                },
                "endDate": {
                    "Year": 2024,
                    "Month": 9,
                    "Day": 29
                }
            },
            "discounts": [
                {
                    "categoryName": "Organic Wine",
                    "discountPercentage": 19
                },
                {
                    "categoryName": "White Wine",
                    "discountPercentage": 20
                },
                {
                    "categoryName": "Sparkling Wine",
                    "discountPercentage": 19
                },
                {
                    "categoryName": "Whiskey",
                    "discountPercentage": 17
                },
                {
                    "categoryName": "Vodka",
                    "discountPercentage": 23
                }
            ]
        }
    ]
}

示例 1 - 查找促销活动名称包含子字符串“Days”的所有商店

db.stores.find({"promotionEvents.eventName": {"$regex": /Days/}}, {"name": 1}, {"limit": 3})

这会返回以下结果:

[
  {
    "_id": "40d6f4d7-50cd-4929-9a07-0a7a133c2e74",
    "name": "Proseware, Inc. | Home Entertainment Hub - East Linwoodbury"
  },
  {
    "_id": "f2a8c190-28e4-4e14-9d8b-0256e53dca66",
    "name": "Fabrikam, Inc. | Car Accessory Outlet - West Adele"
  },
  {
    "_id": "65300639-9bf0-460c-ae1f-891b2ff479b1",
    "name": "Wide World Importers | Fitness Equipment Emporium - Reillyborough"
  }
]

示例 2 - 执行不区分大小写的正则表达式模式匹配

查找具有促销事件的所有商店,其中名称包含不区分大小写的子字符串“days”

db.stores.find({"promotionEvents.eventName": {"$regex": /bash/i}})

这会返回以下结果:

[
  {
    "_id": "40d6f4d7-50cd-4929-9a07-0a7a133c2e74",
    "name": "Proseware, Inc. | Home Entertainment Hub - East Linwoodbury"
  },
  {
    "_id": "438db151-04b8-4422-aa97-acf94bc69cfc",
    "name": "Fourth Coffee | Turntable Boutique - Tromptown"
  },
  {
    "_id": "c5041337-bd61-4efa-bc7a-02799a2ce82c",
    "name": "Wide World Importers | Headphone Corner - McGlynnview"
  }
]