運算子會$anyElementTrue將陣列評估為集合,並在陣列中的任何元素為 true (或等同true於 ) 時傳回true。 如果所有元素的評估值 false為 、 null、 0或 undefined,運算子會傳回 false。
語法
{
$anyElementTrue: [ <array> ]
}
參數
| 參數 | Description |
|---|---|
array |
要評估的表達式陣列。 如果陣列是空的, $anyElementTrue 則傳 false回 。 |
範例
請參考商店集合中的此範例檔。
{
"_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:判斷是否有任何銷售類別超過目標
此查詢會判斷是否有任何銷售類別超過指定的目標。 在這種情況下,目標是銷售額為 40,000 輛。
db.stores.aggregate([
{ $match: {"_id": "40d6f4d7-50cd-4929-9a07-0a7a133c2e74"} },
{
$project: {
salesByCategory: "$sales.salesByCategory",
hasHighPerformingCategory: {
$anyElementTrue: [
{
$map: {
input: "$sales.salesByCategory",
as: "category",
in: { $gt: ["$$category.totalSales", 40000] }
}
}
]
}
}
}
])
此查詢會傳回下列結果。
[
{
"_id": "40d6f4d7-50cd-4929-9a07-0a7a133c2e74",
"salesByCategory": [
{
"categoryName": "Sound Bars",
"totalSales": 2120,
"lastUpdated": "2025-06-11T11:10:34.414Z"
},
null,
{
"categoryName": "Game Controllers",
"totalSales": 43522
},
{
"categoryName": "Remote Controls",
"totalSales": 28946
},
{
"categoryName": "VR Games",
"totalSales": 32272
}
],
"hasHighPerformingCategory": true
}
]
範例 2:判斷是否有任何促銷活動有高折扣
此查詢會判斷是否有任何促銷活動提供超過 20%的折扣。
db.stores.aggregate([
{ $match: {"_id": "40d6f4d7-50cd-4929-9a07-0a7a133c2e74"} },
{ $unwind: "$promotionEvents" },
{
$project: {
eventName: "$promotionEvents.eventName",
hasHighDiscount: {
$anyElementTrue: [
{
$map: {
input: "$promotionEvents.discounts",
as: "discount",
in: { $gte: ["$$discount.discountPercentage", 20] }
}
}
]
}
}
}
])
此查詢傳回的前五個結果如下:
[
{
"_id": "40d6f4d7-50cd-4929-9a07-0a7a133c2e74",
"eventName": "Massive Markdown Mania",
"hasHighDiscount": true
},
{
"_id": "40d6f4d7-50cd-4929-9a07-0a7a133c2e74",
"eventName": "Fantastic Deal Days",
"hasHighDiscount": true
},
{
"_id": "40d6f4d7-50cd-4929-9a07-0a7a133c2e74",
"eventName": "Discount Delight Days",
"hasHighDiscount": true
},
{
"_id": "40d6f4d7-50cd-4929-9a07-0a7a133c2e74",
"eventName": "Super Sale Spectacular",
"hasHighDiscount": true
},
{
"_id": "40d6f4d7-50cd-4929-9a07-0a7a133c2e74",
"eventName": "Grand Deal Days",
"hasHighDiscount": true
}
]