$bottom

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

構文

{
    $bottom: {
        output: [listOfFields],
        sortBy: {
            <fieldName>: < sortOrder >
        }
    }
}

パラメーター

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

例示

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: 売上合計が最も低いストアを検索する

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

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

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

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

例 2: 売上が最も低い店舗ごとのカテゴリを検索する

店舗あたりの売上が最も低いカテゴリを検索するには、クエリを実行して複数の販売カテゴリを含む店舗を取得し、各店舗内の売上合計の降順でカテゴリを並べ替え、並べ替えられた結果セットの最後のドキュメントを返します。

db.stores.aggregate([{
        $unwind: "$sales.salesByCategory"
    },
    {
        $match: {
            "sales.salesByCategory.totalSales": {
                $exists: true
            }
        }
    },
    {
        $group: {
            _id: "$_id",
            storeName: {
                $first: "$name"
            },
            lowestCategory: {
                $bottom: {
                    sortBy: {
                        "sales.salesByCategory.totalSales": 1
                    },
                    output: {
                        categoryName: "$sales.salesByCategory.categoryName",
                        totalSales: "$sales.salesByCategory.totalSales"
                    }
                }
            }
        }
    }
])

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

[
  {
    "_id": "b1d86d1f-8705-4157-b64c-a9eda0df4921",
    "storeName": "VanArsdel, Ltd. | Baby Products Haven - West Kingfort",
    "lowestCategory": { "categoryName": "Baby Monitors", "totalSales": 49585 }
  },
  {
    "_id": "22e6367e-8341-415f-9795-118d2b522abf",
    "storeName": "Adatum Corporation | Outdoor Furniture Mart - Port Simone",
    "lowestCategory": { "categoryName": "Outdoor Benches", "totalSales": 4976 }
  }
]