$bottomN

$bottomN演算子は、クエリで指定された 1 つ以上のフィールドのドキュメントを並べ替え、フィルター条件に一致する最後の N 個のドキュメントを返します。

構文

{
    $bottomN: {
        output: [listOfFields],
        sortBy: {
            <fieldName>: < sortOrder >
        },
        n: < numDocumentsToReturn >
    }
}

パラメーター

パラメーター Description
listOfFields 結果セット内の最後のドキュメントに対して返されるフィールドの一覧
fieldName 結果セットの並べ替えに使用するフィールド
sortOrder 1 または -1。 1 はフィールドの値の昇順で並べ替えを意味し、-1 はフィールドの値の降順で並べ替えを意味します
n 並べ替えられた結果セットの下部から返されるドキュメントの数

例示

stores コレクションのこのサンプル ドキュメントについて考えてみましょう。

{
    "_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: 売上合計で下位 2 店舗を検索する

売上が最も低い First Up Consultants 企業の 2 つの店舗を特定するには、クエリを実行して "First Up Consultants" 企業内の店舗を取得し、結果のドキュメントを合計売上の降順で並べ替え、並べ替えられた結果セットから最後の 2 つのドキュメントを返します。

db.stores.aggregate([{
    $match: {
        company: {
            $in: ["First Up Consultants"]
        }
    }
}, {
    $group: {
        _id: "$company",
        bottomSales: {
            $bottomN: {
                output: ["$company", "$sales"],
                sortBy: {
                    "sales.revenue": -1
                },
                n: 2
            }
        }
    }
}])

このクエリは、次の結果を返します。

[
  {
    "_id": "First Up Consultants",
    "bottomSales": [
        [
            "First Up Consultants",
            {
                "salesByCategory": [
                    {
                        "categoryName": "Skirts",
                        "totalSales": 109
                    }
                ],
                "revenue": 109
            }
        ],
        [
            "First Up Consultants",
            {
                "salesByCategory": [
                    {
                        "categoryName": "Mirrors",
                        "totalSales": 120
                    }
                ],
                "revenue": 120
            }
        ]
    ]
}
]

例 2: 各ストア内の売上合計で下位 2 つのカテゴリを検索する

各ストア内の売上合計で最もパフォーマンスの低い 2 つのカテゴリを決定するには、クエリを実行して、少なくとも 2 つの販売カテゴリを含むドキュメントを取得し、合計売上の降順でカテゴリを並べ替え、最後にストアごとに下位 2 つのカテゴリを返します。

db.stores.aggregate([{
    $unwind: "$sales.salesByCategory"
}, {
    $match: {
        "sales.salesByCategory.totalSales": {
            $exists: true
        }
    }
}, {
    $group: {
        _id: "$_id",
        storeName: {
            $first: "$name"
        },
        categoryCount: {
            $sum: 1
        },
        bottomTwoCategories: {
            $bottomN: {
                n: 2,
                sortBy: {
                    "sales.salesByCategory.totalSales": -1
                },
                output: {
                    categoryName: "$sales.salesByCategory.categoryName",
                    totalSales: "$sales.sale"
                }
            }
        }
    }
}, {
    $match: {
        categoryCount: {
            $gte: 2
        }
    }
}])

このクエリによって返された最初の結果は次のとおりです。

[
    {
        "_id": "64ec6589-068a-44a6-be5b-9d37bb0a90f1",
        "storeName": "First Up Consultants | Computer Gallery - West Cathrine",
        "categoryCount": 8,
        "bottomTwoCategories": [
            {
                "categoryName": "Gaming Controllers",
                "totalSales": null
            },
            {
                "categoryName": "Network Adapters",
                "totalSales": null
            }
        ]
    }
]