집계 파이프라인의 $skip 단계는 입력에서 지정된 수의 문서를 건너뛰고 나머지 문서를 파이프라인의 다음 단계로 전달하는 데 사용됩니다. 이 단계는 쿼리에서 페이지 매김을 구현하고 파이프라인의 후속 단계에서 작동하는 문서의 하위 집합을 제어하는 데 유용합니다.
문법
{
$skip: <number>
}
매개 변수
| 매개 변수 | Description |
|---|---|
number |
나머지 문서를 다음 단계로 전달하기 전에 건너뛸 문서 수입니다. |
예시
스토어 컬렉션에서 이 샘플 문서를 고려합니다.
{
_id: '0fcc0bf0-ed18-4ab8-b558-9848e18058f4',
name: 'First Up Consultants | Beverage Shop - Satterfieldmouth',
location: { lat: -89.2384, lon: -46.4012 },
staff: { employeeCount: { fullTime: 2, partTime: 20 } },
sales: {
salesByCategory: [
{ categoryName: 'Wine Accessories', totalSales: 34450 },
{ categoryName: 'Bitters', totalSales: 39496 },
{ categoryName: 'Rum', totalSales: 1734 }
],
revenue: 75670
},
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 }
]
},
{
eventName: 'Summer Sale',
promotionalDates: {
startDate: { Year: 2024, Month: 6, Day: 1 },
endDate: { Year: 2024, Month: 6, Day: 15 }
},
discounts: [ { categoryName: 'DJ Speakers', discountPercentage: 20 } ]
}
],
company: 'First Up Consultants',
city: 'Satterfieldmouth',
storeOpeningDate: ISODate("2024-09-20T18:28:57.302Z"),
lastUpdated: Timestamp({ t: 1729448937, i: 1 }),
store: { promotionEvents: null },
tag: [ '#ShopLocal' ]
}
예제 1: 컬렉션에서 문서 건너뛰기
처음 두 문서를 건너뛰고 나머지는 반환하려면, 다음 집계 파이프라인을 사용할 수 있습니다.
db.stores.aggregate([
{ $skip: 2 }
])
이 쿼리에서 반환된 처음 두 가지 결과는 다음과 같습니다.
[
{
"_id": "7954bd5c-9ac2-4c10-bb7a-2b79bd0963c5",
"store": {
"name": "Downtown Store",
"promotionEvents": ["Summer Sale", "Black Friday", "Holiday Deals"]
}
},
{
"_id": "7954bd5c-9ac2-4c10-bb7a-2b79bd0963c6",
"store": {
"name": "Uptown Store",
"promotionEvents": ["Back to School", "Winter Sale"]
}
}
]
예제 2: 문서를 건너뛰고 결과 제한
처음 2개의 문서를 건너뛰고 결과를 다음 3개 문서로 제한하려면 $skip 및 $limit 단계를 결합하면 됩니다.
db.stores.aggregate([
{ $skip: 2 },
{ $limit: 3 }
])
이 쿼리에서 반환된 처음 두 가지 결과는 다음과 같습니다.
[
{
"_id": "728c068a-638c-40af-9172-8ccfa7dddb49",
"name": "Contoso, Ltd. | Book Store - Lake Myron",
"location": {
"lat": 29.416,
"lon": 21.5231
},
"staff": {
"employeeCount": {
"fullTime": 7,
"partTime": 16
}
},
"sales": {
"salesByCategory": [
{
"categoryName": "Science Fiction",
"totalSales": 34879
}
],
"revenue": 34879
},
"promotionEvents": [
{
"eventName": "Blowout Bonanza",
"promotionalDates": {
"startDate": {
"Year": 2024,
"Month": 9,
"Day": 21
},
"endDate": {
"Year": 2024,
"Month": 9,
"Day": 30
}
},
"discounts": [
{
"categoryName": "Children's Books",
"discountPercentage": 11
},
{
"categoryName": "Fiction",
"discountPercentage": 21
}
]
}
],
"company": "Contoso, Ltd.",
"city": "Lake Myron",
"storeOpeningDate": "ISODate('2024-09-28T18:23:21.591Z')",
"lastUpdated": "Timestamp({ t: 1730139801, i: 1 })",
"storeFeatures": 239
},
{
"_id": "44fdb9b9-df83-4492-8f71-b6ef648aa312",
"name": "Fourth Coffee | Storage Solution Gallery - Port Camilla",
"location": {
"lat": 78.3889,
"lon": 0.6784
},
"staff": {
"employeeCount": {
"fullTime": 17,
"partTime": 15
}
},
"sales": {
"salesByCategory": [
{
"categoryName": "Storage Boxes",
"totalSales": 2236
}
],
"revenue": 2236
},
"promotionEvents": [
{
"eventName": "Major Discount Mania",
"promotionalDates": {
"startDate": {
"Year": 2024,
"Month": 6,
"Day": 23
},
"endDate": {
"Year": 2024,
"Month": 7,
"Day": 3
}
},
"discounts": [
{
"categoryName": "Bathroom Storage",
"discountPercentage": 19
},
{
"categoryName": "Kitchen Storage",
"discountPercentage": 10
}
]
},
{
"eventName": "Flash Deal Frenzy",
"promotionalDates": {
"startDate": {
"Year": 2024,
"Month": 9,
"Day": 21
},
"endDate": {
"Year": 2024,
"Month": 9,
"Day": 30
}
},
"discounts": [
{
"categoryName": "Under-Bed Storage",
"discountPercentage": 20
},
{
"categoryName": "Closet Organizers",
"discountPercentage": 21
}
]
}
],
"company": "Fourth Coffee",
"city": "Port Camilla",
"storeOpeningDate": "ISODate('2024-09-23T06:02:53.844Z')",
"lastUpdated": "Timestamp({ t: 1729663373, i: 1 })",
"storeFeatures": 222
}
]
예제 3: 복잡한 파이프라인에서 문서 건너뛰기
첫 번째 승격 이벤트를 건너뛰고 특정 저장소에 대한 나머지 이벤트를 프로젝팅하려면 다음을 수행합니다.
db.stores.aggregate([
{ $match: { "_id": "0fcc0bf0-ed18-4ab8-b558-9848e18058f4", } },
{ $unwind: "$promotionEvents" },
{ $skip: 1 },
{ $project: { "promotionEvents": 1, _id: 0 } }
])
이 쿼리에서 반환된 처음 두 가지 결과는 다음과 같습니다.
[
{
"promotionEvents": {
"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
}
]
}
},
{
"promotionEvents": {
"eventName": "Summer Sale",
"promotionalDates": {
"startDate": {
"Year": 2024,
"Month": 6,
"Day": 1
},
"endDate": {
"Year": 2024,
"Month": 6,
"Day": 15
}
},
"discounts": [
{
"categoryName": "DJ Speakers",
"discountPercentage": 20
}
]
}
}
]