정의
버전 5.0의 신규 항목입니다.
4} 스테이지 $setWindowFields 파티션의 현재 문서를 기준으로 지정된 위치에 있는 문서에 적용된 표현식의 값을 반환합니다.
$setWindowFields 단계 sortBy 필드 값은 문서 순서를 결정합니다. MongoDB가 서로 다른 유형의 필드를 비교하는 방법에 대한 자세한 내용은 BSON 비교 순서를 참조하세요.
$shift는 $setWindowFields 단계에서만 사용할 수 있습니다.
$shift 구문:
{ $shift: { output: <output expression>, by: <integer>, default: <default expression> } }
$shift (은)는 다음 필드가 있는 문서를 가져옵니다.
필드 | 설명 |
|---|---|
평가하고 출력에서 반환할 표현식 을 지정합니다. | |
출력의 현재 문서를 기준으로 한 숫자 문서 위치를 사용하여 예를 들면 다음과 같습니다.
| |
문서 위치가 암시적 The default expression must evaluate to a constant value. If you do not specify a default expression, |
행동
$shift returns an error if you specify a window in the $setWindowFields stage.
예시
캘리포니아주(CA)와 워싱턴주(WA)의 케이크 판매량이 포함된 cakeSales collection을 생성합니다.
db.cakeSales.insertMany( [ { _id: 0, type: "chocolate", orderDate: new Date("2020-05-18T14:10:30Z"), state: "CA", price: 13, quantity: 120 }, { _id: 1, type: "chocolate", orderDate: new Date("2021-03-20T11:30:05Z"), state: "WA", price: 14, quantity: 140 }, { _id: 2, type: "vanilla", orderDate: new Date("2021-01-11T06:31:15Z"), state: "CA", price: 12, quantity: 145 }, { _id: 3, type: "vanilla", orderDate: new Date("2020-02-08T13:13:23Z"), state: "WA", price: 13, quantity: 104 }, { _id: 4, type: "strawberry", orderDate: new Date("2019-05-18T16:09:01Z"), state: "CA", price: 41, quantity: 162 }, { _id: 5, type: "strawberry", orderDate: new Date("2019-01-08T06:12:03Z"), state: "WA", price: 43, quantity: 134 } ] )
다음 예시에서는 cakeSales 컬렉션이 사용됩니다.
양의 정수를 사용하여 이동
This example uses $shift in the $setWindowFields stage to output the quantity of the cake sales from each document following the current document for each state:
db.cakeSales.aggregate( [ { $setWindowFields: { partitionBy: "$state", sortBy: { quantity: -1 }, output: { shiftQuantityForState: { $shift: { output: "$quantity", by: 1, default: "Not available" } } } } } ] )
예시:
partitionBy: "$state"는 collection의 문서를state로 파티셔닝합니다.CA및WA에 대한 파티션이 있습니다.sortBy: { quantity: -1 }각 파티션의 문서를quantity별로 내림차순(-1)으로 정렬하므로 가장 높은quantity이(가) 첫 번째입니다.
outputsortBy이후 :shiftQuantityForState필드를 각state의 문서에서quantity값으로 설정합니다.Uses
$shiftto return thequantityvalue from the document that follows the current document in the output.
이 예제 출력에서는 반환된 각 문서에 대해 이동된 quantity 값이 shiftQuantityForState 필드에 표시됩니다.
{ "_id" : 4, "type" : "strawberry", "orderDate" : ISODate("2019-05-18T16:09:01Z"), "state" : "CA", "price" : 41, "quantity" : 162, "shiftQuantityForState" : 145 } { "_id" : 2, "type" : "vanilla", "orderDate" : ISODate("2021-01-11T06:31:15Z"), "state" : "CA", "price" : 12, "quantity" : 145, "shiftQuantityForState" : 120 } { "_id" : 0, "type" : "chocolate", "orderDate" : ISODate("2020-05-18T14:10:30Z"), "state" : "CA", "price" : 13, "quantity" : 120, "shiftQuantityForState" : "Not available" } { "_id" : 1, "type" : "chocolate", "orderDate" : ISODate("2021-03-20T11:30:05Z"), "state" : "WA", "price" : 14, "quantity" : 140, "shiftQuantityForState" : 134 } { "_id" : 5, "type" : "strawberry", "orderDate" : ISODate("2019-01-08T06:12:03Z"), "state" : "WA", "price" : 43, "quantity" : 134, "shiftQuantityForState" : 104 } { "_id" : 3, "type" : "vanilla", "orderDate" : ISODate("2020-02-08T13:13:23Z"), "state" : "WA", "price" : 13, "quantity" : 104, "shiftQuantityForState" : "Not available" }
음수 정수를 사용하여 이동
This example uses $shift in the $setWindowFields stage to output the quantity of the cake sales from each document before the current document for each state:
db.cakeSales.aggregate( [ { $setWindowFields: { partitionBy: "$state", sortBy: { quantity: -1 }, output: { shiftQuantityForState: { $shift: { output: "$quantity", by: -1, default: "Not available" } } } } } ] )
예시:
partitionBy: "$state"는 collection의 문서를state로 파티셔닝합니다.CA및WA에 대한 파티션이 있습니다.sortBy: { quantity: -1 }각 파티션의 문서를quantity별로 내림차순(-1)으로 정렬하므로 가장 높은quantity이(가) 첫 번째입니다.
outputsortBy이후 :shiftQuantityForState필드를 각state의 문서에서quantity값으로 설정합니다.Uses
$shiftto return thequantityvalue from the document before the current document in the output.
이 예제 출력에서는 반환된 각 문서에 대해 이동된 quantity 값이 shiftQuantityForState 필드에 표시됩니다.
{ "_id" : 4, "type" : "strawberry", "orderDate" : ISODate("2019-05-18T16:09:01Z"), "state" : "CA", "price" : 41, "quantity" : 162, "shiftQuantityForState" : "Not available" } { "_id" : 2, "type" : "vanilla", "orderDate" : ISODate("2021-01-11T06:31:15Z"), "state" : "CA", "price" : 12, "quantity" : 145, "shiftQuantityForState" : 162 } { "_id" : 0, "type" : "chocolate", "orderDate" : ISODate("2020-05-18T14:10:30Z"), "state" : "CA", "price" : 13, "quantity" : 120, "shiftQuantityForState" : 145 } { "_id" : 1, "type" : "chocolate", "orderDate" : ISODate("2021-03-20T11:30:05Z"), "state" : "WA", "price" : 14, "quantity" : 140, "shiftQuantityForState" : "Not available" } { "_id" : 5, "type" : "strawberry", "orderDate" : ISODate("2019-01-08T06:12:03Z"), "state" : "WA", "price" : 43, "quantity" : 134, "shiftQuantityForState" : 140 } { "_id" : 3, "type" : "vanilla", "orderDate" : ISODate("2020-02-08T13:13:23Z"), "state" : "WA", "price" : 13, "quantity" : 104, "shiftQuantityForState" : 134 }