$allElementsTrue 연산자는 배열을 집합으로 평가합니다. 배열 true 에 값이 없거나 해당하는 false 요소(예: false, null또는 0)가 없는 경우 반환 undefined 됩니다. 요소가 값 또는 해당 값 false 으로 계산되면 연산자가 반환됩니다 false.
문법
{
$allElementsTrue: [ <array> ]
}
매개 변수
| 매개 변수 | Description |
|---|---|
array |
평가할 식의 배열입니다. 배열이 비어 있으면 .를 $allElementsTrue 반환합니다 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: 모든 할인 비율이 0보다 높은지 확인
이 쿼리는 각 프로모션 이벤트의 모든 할인 비율이 0보다 큰지 확인합니다.
db.stores.aggregate([
{ $match: {"_id": "2cf3f885-9962-4b67-a172-aa9039e9ae2f"} },
{ $unwind: "$promotionEvents" },
{
$project: {
allDiscountsValid: {
$allElementsTrue: [
{
$map: {
input: "$promotionEvents.discounts.discountPercentage",
as: "discount",
in: { $gt: ["$$discount", 0] }
}
}
]
}
}
}
])
이 쿼리는 다음 결과를 반환합니다.
[
{
"_id": "2cf3f885-9962-4b67-a172-aa9039e9ae2f",
"allDiscountsValid": true
}
]