$slice演算子は、配列のサブセットを返すために使用されます。 配列内の要素の数を指定した数に制限したり、配列内の指定した位置から要素を返したりするために使用できます。 この演算子は、処理または表示にデータの一部のみが必要な大きな配列を処理する場合に便利です。
構文
$slice演算子の構文は次のとおりです。
- 配列の先頭または末尾から要素を返します。
{
$slice: [ <array>, <n> ]
}
- 配列内の指定した位置から要素を返します。
{
$slice: [ <array>, <position>, <n> ]
}
パラメーター
| パラメーター | Description |
|---|---|
array |
任意の有効な配列式。 |
position |
任意の有効な整数式。 |
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: 配列から要素のセットを返す
このクエリは、コレクション内のsales.salesByCategoryの_id: 988d2dd1-2faa-4072-b420-b91b95cbfd60配列の最初の 3 つの要素stores取得します。
db.stores.aggregate([{
$match: {
_id: "988d2dd1-2faa-4072-b420-b91b95cbfd60"
}
}, {
$project: {
salesByCategory: {
$slice: ["$sales.salesByCategory", 3]
}
}
}])
このクエリは、次の結果を返します。
[
{
"_id": "988d2dd1-2faa-4072-b420-b91b95cbfd60",
"salesByCategory": [
{
"categoryName": "Towel Racks",
"totalSales": 13237
},
{
"categoryName": "Washcloths",
"totalSales": 44315
},
{
"categoryName": "Face Towels",
"totalSales": 42095
}
]
}
]
例 2: $pushを含むスライス
このクエリでは、 $push と $each を使用して新しい要素を promotionEvents 配列に追加し、最初の N (正のスライス) 要素または最後の N (負のスライス) 要素のみを保持する $slice します。 これにより、更新後に配列に最新のエントリが保持されます。
db.stores.updateOne({
_id: "7954bd5c-9ac2-4c10-bb7a-2b79bd0963c5"
}, {
$push: {
promotionEvents: {
$each: [{
eventName: "Black Friday Event",
promotionalDates: {
startDate: {
Year: 2024,
Month: 8,
Day: 1
},
endDate: {
Year: 2024,
Month: 8,
Day: 7
}
},
discounts: [{
categoryName: 'DJ Speakers',
discountPercentage: 25
}]
},
{
eventName: "Mega Discount Days",
promotionalDates: {
startDate: {
Year: 2024,
Month: 5,
Day: 11
},
endDate: {
Year: 2024,
Month: 5,
Day: 18
}
},
discounts: [{
categoryName: "DJ Lights",
discountPercentage: 20
}]
}
],
$slice: -3
}
}
})
クエリは次の結果を返します。
[
{
"_id": "7954bd5c-9ac2-4c10-bb7a-2b79bd0963c5",
"name": "Lakeshore Retail | DJ Equipment Stop - Port Cecile",
"location": {
"lat": 60.1441,
"lon": -141.5012
},
"staff": {
"totalStaff": {
"fullTime": 2,
"partTime": 0
}
},
"sales": {
"salesByCategory": [
{
"categoryName": "DJ Headphones",
"totalSales": 35921
},
{
"categoryName": "DJ Cables",
"totalSales": 1000
}
],
"fullSales": 3700
},
"promotionEvents": [
{
"eventName": "Cyber Monday Event",
"promotionalDates": {
"startDate": {
"Year": 2024,
"Month": 8,
"Day": 1
},
"endDate": {
"Year": 2024,
"Month": 8,
"Day": 7
}
},
"discounts": [
{
"categoryName": "DJ Speakers",
"discountPercentage": 25
}
]
},
{
"eventName": "Black Friday Event",
"promotionalDates": {
"startDate": {
"Year": 2024,
"Month": 8,
"Day": 1
},
"endDate": {
"Year": 2024,
"Month": 8,
"Day": 7
}
},
"discounts": [
{
"categoryName": "DJ Speakers",
"discountPercentage": 25
}
]
},
{
"eventName": "Mega Discount Days",
"promotionalDates": {
"startDate": {
"Year": 2024,
"Month": 5,
"Day": 11
},
"endDate": {
"Year": 2024,
"Month": 5,
"Day": 18
}
},
"discounts": [
{
"categoryName": "DJ Lights",
"discountPercentage": 20
}
]
}
],
"tag": [
"#ShopLocal",
"#NewArrival",
"#FashionStore",
"#SeasonalSale",
"#FreeShipping",
"#MembershipDeals"
]
}
]
例 3: 配列から最初に一致する要素をフェッチする
このクエリは、"sales.salesByCategory" 配列から最初のドキュメントを取得します。
db.stores.find({
name: "Lakeshore Retail"
}, {
_id: 1,
name: 1,
"sales.salesByCategory": {
$slice: 1
}
} // restricts the fields to be returned
)
このクエリは、次の結果を返します。
[
{
"_id": "988d2dd1-2faa-4072-b420-b91b95cbfd60",
"name": "Lakeshore Retail",
"sales": {
"salesByCategory": [
{
"categoryName": "Towel Racks",
"totalSales": 13237
}
]
}
}
]
例 4: 配列から最後の要素をフェッチする
このクエリは、"sales.salesByCategory" 配列から最後のドキュメントを取得します。
db.stores.find({
name: "Lakeshore Retail"
}, {
_id: 1,
name: 1,
"sales.salesByCategory": {
$slice: -1
}
})
このクエリは、次の結果を返します。
[
{
"_id": "988d2dd1-2faa-4072-b420-b91b95cbfd60",
"name": "Lakeshore Retail",
"sales": {
"salesByCategory": [
{
"categoryName": "Pillow Cases",
"totalSales": 38833
}
]
}
}
]
例 5: 配列から要素の範囲を取得する
このクエリは、"sales.salesByCategory" 配列からサブセット範囲を取得します。
db.stores.find({
name: "Lakeshore Retail"
}, {
_id: 1,
name: 1,
"sales.salesByCategory": {
$slice: [3, 2]
}
})
このクエリは、次の結果を返します。
[
{
"_id": "988d2dd1-2faa-4072-b420-b91b95cbfd60",
"name": "Lakeshore Retail",
"sales": {
"salesByCategory": [
{
"categoryName": "Toothbrush Holders",
"totalSales": 47912
},
{
"categoryName": "Hybrid Mattresses",
"totalSales": 48660
}
]
}
}
]
関連コンテンツ
- MongoDB から Azure DocumentDB に移行するためのオプションを確認します。
- MongoDB との機能の互換性について詳しくは、こちらをご覧ください。