$firstN 演算子を使用すると、グループの並べ替え順序に従って、グループ内の最初の N 個の値が返されます。 並べ替え順序が指定されていない場合、順序は未定義です。
構文
{
$firstN: {
input: [listOfFields],
sortBy: {
<fieldName>: <sortOrder>
},
n: <numDocumentsToReturn>
}
}
パラメーター
| パラメーター | Description |
|---|---|
listOfFields |
結果セット内の最初の N 個のドキュメントに対して返されるフィールドの一覧 |
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: アキュムレータとして $firstN 演算子を使用して、売上合計で最初の 2 つの店舗を検索する
売上合計で上位 2 つのストアを取得するには、クエリを実行して sales.totalSales の降順ですべてのドキュメントを並べ替え、並べ替えられた結果セットから最初の 2 つのドキュメントを返します。
db.stores.aggregate([{
$sort: {
"sales.totalSales": -1
}
},
{
$group: {
_id: null,
topTwoStores: {
$firstN: {
n: 2,
input: {
storeId: "$_id",
storeName: "$name",
totalSales: "$sales.totalSales"
}
}
}
}
}
])
このクエリは、次の結果を返します。
[
{
"_id": null,
"topTwoStores": [
{
"storeId": "ffe155dd-caa2-4ac1-8ec9-0342241a84a3",
"storeName": "Lakeshore Retail | Electronics Stop - Vicentastad",
"totalSales": 399426
},
{
"storeId": "cba62761-10f8-4379-9eea-a9006c667927",
"storeName": "Fabrikam, Inc. | Electronics Nook - East Verlashire",
"totalSales": 374845
}
]
}
]
例 2: $firstN演算子をアキュムレータとして使用して、ストアごとに最初の 2 つのカテゴリを検索する
各ストア内の売上合計によって最初の 2 つのカテゴリを取得するには、クエリを実行して、各ストアのスコープ内で sales.totalSales の降順ですべてのドキュメントを並べ替え、ストアごとに並べ替えられた結果セットから最初の 2 つのカテゴリを返します。
db.stores.aggregate([
{ $unwind: "$sales.salesByCategory" },
{
$match: {
"sales.salesByCategory.totalSales": { $exists: true }
}
},
{
$sort: {
"_id": 1,
"sales.salesByCategory.totalSales": -1
}
},
{
$group: {
_id: "$_id",
storeName: { $first: "$name" },
categoryCount: { $sum: 1 },
firstTwoCategories: {
$push: {
categoryName: "$sales.salesByCategory.categoryName",
totalSales: "$sales.salesByCategory.totalSales"
}
}
}
},
{
$project: {
storeName: 1,
categoryCount: 1,
firstTwoCategories: { $slice: ["$firstTwoCategories", 2] }
}
},
{
$match: {
categoryCount: { $gte: 2 }
}
},
{ $limit: 2 }
])
このクエリによって返される最初の 2 つの結果は次のとおりです。
[
{
"_id": "2e07b49d-1730-491b-b847-44b6a34812c1",
"storeName": "VanArsdel, Ltd. | Electronics Market – North Bransonborough",
"categoryCount": 3,
"firstTwoCategories": [
{
"categoryName": "iPads",
"totalSales": 37113
},
{
"categoryName": "Laptops",
"totalSales": 9175
}
]
},
{
"_id": "1bec7539-dc75-4f7e-b4e8-afdf8ff2f234",
"storeName": "Adatum Corporation | Health Food Market – East Karina",
"categoryCount": 2,
"firstTwoCategories": [
{
"categoryName": "Protein Bars",
"totalSales": 49900
},
{
"categoryName": "Superfoods",
"totalSales": 39683
}
]
}
]
例 3: 配列式として firstN 演算子を使用して、最初の 3 つの販売カテゴリを検索する
この例では、分析用の最初の 3 つの販売カテゴリを検索するオペレーターの使用状況を示します。
db.stores.aggregate([
{ $match: {"_id": "40d6f4d7-50cd-4929-9a07-0a7a133c2e74"} },
{
$project: {
name: 1,
totalSales: "$sales.totalSales",
firstThreeCategories: {
$firstN: {
input: "$sales.salesByCategory",
n: 3
}
}
}
}
])
このクエリは、次の結果を返します。
[
{
"_id": "40d6f4d7-50cd-4929-9a07-0a7a133c2e74",
"name": "Proseware, Inc. | Home Entertainment Hub - East Linwoodbury",
"totalSales": 165000,
"firstThreeCategories": [
{
"categoryName": "Sound Bars",
"totalSales": 2120
},
null,
{
"categoryName": "Game Controllers",
"totalSales": 43522
}
]
}
]