運算子 $and 會在表達式陣列上執行邏輯AND運算,並擷取滿足所有表達式的檔。
語法
{
$and: [{
< 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:使用 AND 運算子作為邏輯查詢
此查詢會使用運算子篩選 $and 全職員工人數大於 10 且兼職員工人數少於 15 的商店。 它只會 name 投影 和 staff 欄位,並將結果限製為三筆記錄。
db.stores.find({
$and: [{
"staff.employeeCount.fullTime": {
$gt: 10
}
}, {
"staff.employeeCount.partTime": {
$lt: 15
}
}]
}, {
"name": 1,
"staff": 1
}).limit(3)
此查詢傳回的前三個結果為:
[
{
"_id": "e60c807b-d31c-4903-befb-5d608f260ba3",
"name": "Wide World Importers | Appliance Emporium - Craigfort",
"staff": {
"totalStaff": {
"fullTime": 11,
"partTime": 8
}
}
},
{
"_id": "70032165-fded-47b4-84a3-8d9c18a4d1e7",
"name": "Northwind Traders | Picture Frame Bazaar - Lake Joesph",
"staff": {
"totalStaff": {
"fullTime": 14,
"partTime": 0
}
}
},
{
"_id": "dda2a7d2-6984-40cc-bbea-4cbfbc06d8a3",
"name": "Contoso, Ltd. | Home Improvement Closet - Jaskolskiview",
"staff": {
"totalStaff": {
"fullTime": 16,
"partTime": 8
}
}
}
]
範例 2:使用 AND 運算子作為布爾運算式來尋找具有高銷售額和足夠員工的商店
此查詢會尋找總銷售額超過 100,000 人且員工總數超過 30 人的商店。
db.stores.aggregate([
{
$project: {
name: 1,
totalSales: "$sales.totalSales",
totalStaff: {
$add: ["$staff.employeeCount.fullTime", "$staff.employeeCount.partTime"]
},
meetsHighPerformanceCriteria: {
$and: [
{ $gt: ["$sales.totalSales", 100000] },
{ $gt: [{ $add: ["$staff.employeeCount.fullTime", "$staff.employeeCount.partTime"] }, 30] }
]
}
}
},
{ $limit: 2 }
])
此查詢傳回的前兩個結果是:
[
{
"_id": "905d1939-e03a-413e-a9c4-221f74055aac",
"name": "Trey Research | Home Office Depot - Lake Freeda",
"totalStaff": 31,
"meetsHighPerformanceCriteria": false
},
{
"_id": "a715ab0f-4c6e-4e9d-a812-f2fab11ce0b6",
"name": "Lakeshore Retail | Holiday Supply Hub - Marvinfort",
"totalStaff": 27,
"meetsHighPerformanceCriteria": false
}
]
相關內容
- 檢視從 MongoDB 遷移到 Azure DocumentDB 的選項。
- 閱讀有關 與 MongoDB 的功能相容性的更多資訊。