運算子 $or 會在表達式陣列上執行邏輯 OR 運算,並擷取至少滿足其中一個指定條件的檔。
語法
{
$or: [{
< expression1 >
}, {
< expression2 >
}, ..., {
< expressionN >
}]
}
參數
| 參數 | Description |
|---|---|
expression |
表達式的數組,其中至少一個必須為真,文件才能被包含進來。 |
範例
請參考商店集合中的此範例檔。
{
"_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
}
).limit(2)
此查詢傳回的前兩個結果是:
[
{
"_id": "dda2a7d2-6984-40cc-bbea-4cbfbc06d8a3",
"name": "Contoso, Ltd. | Home Improvement Closet - Jaskolskiview",
"staff": {
"employeeCount": {
"fullTime": 16,
"partTime": 8
}
}
},
{
"_id": "44fdb9b9-df83-4492-8f71-b6ef648aa312",
"name": "Fourth Coffee | Storage Solution Gallery - Port Camilla",
"staff": {
"employeeCount": {
"fullTime": 17,
"partTime": 15
}
}
}
]
範例 2:使用 OR 運算子做為布爾運算式來識別具有高銷售額或大型員工的商店
此查詢會擷取總銷售額超過 50,000 人或員工總數超過 25 人的商店。
db.stores.aggregate([
{
$project: {
name: 1,
totalSales: "$sales.totalSales",
totalStaff: {
$add: ["$staff.employeeCount.fullTime", "$staff.employeeCount.partTime"]
},
qualifiesForProgram: {
$or: [
{ $gt: ["$sales.totalSales", 50000] },
{ $gt: [{ $add: ["$staff.employeeCount.fullTime", "$staff.employeeCount.partTime"] }, 25] }
]
}
}
},
{ $limit: 4 }
])
此查詢傳回的前四個結果為:
[
{
"_id": "905d1939-e03a-413e-a9c4-221f74055aac",
"name": "Trey Research | Home Office Depot - Lake Freeda",
"totalStaff": 31,
"qualifiesForProgram": true
},
{
"_id": "a715ab0f-4c6e-4e9d-a812-f2fab11ce0b6",
"name": "Lakeshore Retail | Holiday Supply Hub - Marvinfort",
"totalStaff": 27,
"qualifiesForProgram": true
},
{
"_id": "923d2228-6a28-4856-ac9d-77c39eaf1800",
"name": "Lakeshore Retail | Home Decor Hub - Franciscoton",
"totalStaff": 13,
"qualifiesForProgram": false
},
{
"_id": "7e53ca0f-6e24-4177-966c-fe62a11e9af5",
"name": "Contoso, Ltd. | Office Supply Deals - South Shana",
"totalStaff": 2,
"qualifiesForProgram": false
}
]
效能考量
- 檢閱尋找更佳效能的建議。
- 陣列中的每個
$or條件都會獨立評估 - 盡可能使用索引以提升效能
- 考慮最佳執行的條件順序
- 針對相同的欄位進行多個相等檢查時,應使用
$in而不要用$or。 - 讓條件數目
$or保持合理
- 陣列中的每個
相關內容
- 檢視從 MongoDB 遷移到 Azure DocumentDB 的選項。
- 閱讀有關 與 MongoDB 的功能相容性的更多資訊。