该 $or
运算符对表达式数组执行逻辑 OR作,并检索至少满足指定条件之一的文档。
语法
{
$or: [{
< expression1 >
}, {
< expression2 >
}, ..., {
< expressionN >
}]
}
参数
参数 | DESCRIPTION |
---|---|
expression |
表达式数组,其中至少一个表达式必须为 true 才能包含文档 |
例子
请考虑存储集合中的此示例文档。
{
"_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:基本 OR作
若要查找拥有 15 多名全职员工或 20 多名兼职员工的商店,请在两个条件中使用 $or 运算符运行查询。 然后,仅投影结果集中存储区的名称和员工字段。
db.stores.find({
$or: [{
"staff.employeeCount.fullTime": {
$gt: 15
}
}, {
"staff.employeeCount.partTime": {
$gt: 20
}
}]
}, {
"name": 1,
"staff": 1
})
此查询返回的前两个结果为:
[
{
"_id": "62438f5f-0c56-4a21-8c6c-6bfa479494ad",
"name": "First Up Consultants | Plumbing Supply Shoppe - New Ubaldofort",
"staff": {
"employeeCount": {
"fullTime": 20,
"partTime": 18
}
}
},
{
"_id": "e88f0096-4299-4944-9788-695c40786d97",
"name": "Adatum Corporation | Handbag Shoppe - Lucienneberg",
"staff": {
"employeeCount": {
"fullTime": 19,
"partTime": 2
}
}
}
]
示例 2:具有多个条件和数组的复杂 OR
若要查找拥有 25 名员工的商店,并为游戏控制器、声音棒或家庭影院投影仪提供促销活动,请首先针对特定条件与$or和$and操作员运行查询。 然后,仅投影结果集中商店中的名称、工作人员和 promotionEvents 字段。
db.stores.find({
$or: [{
"sales.salesByCategory.categoryName": {
$in: ["Game Controllers", "Sound Bars", "Home Theater Projectors"]
}
}, {
$and: [{
"staff.totalStaff.fullTime": {
$gt: 10
}
}, {
"staff.totalStaff.partTime": {
$gt: 15
}
}]
}, {
"promotionEvents": {
$elemMatch: {
"eventName": "Super Sale Spectacular",
"discounts.discountPercentage": {
$gt: 15
}
}
}
}]
}, {
"name": 1,
"staff": 1,
"promotionEvents": 1
})
此查询返回的第一个结果为:
[
{
"_id": "83218150-9b41-49aa-98fe-8663fa8ee022",
"name": "Trey Research | Home Entertainment Market - New Baby",
"staff": {
"employeeCount": {
"fullTime": 20,
"partTime": 3
}
},
"promotionEvents": [
{
"eventName": "Super Saver Bash",
"promotionalDates": {
"startDate": {
"Year": 2024,
"Month": 9,
"Day": 21
},
"endDate": {
"Year": 2024,
"Month": 10,
"Day": 1
}
},
"discounts": [
{
"categoryName": "Game Controllers",
"discountPercentage": 12
},
{
"categoryName": "Blu-ray Players",
"discountPercentage": 22
},
{
"categoryName": "Projector Mounts",
"discountPercentage": 5
},
{
"categoryName": "Home Theater Systems",
"discountPercentage": 25
},
{
"categoryName": "Game Accessories",
"discountPercentage": 9
},
{
"categoryName": "Projector Cases",
"discountPercentage": 15
}
]
}
]
}
]
性能注意事项
- 独立计算数组中的每个
$or
条件 - 尽可能使用索引来提高性能
- 考虑最佳执行的条件顺序
-
$in
用于$or
同一字段的多个相等性检查 - 使条件数量
$or
保持合理