Definição
Novidade na versão 5.0.
Retorna o valor de uma expressão aplicada a um documento em uma posição especificada em relação ao documento atual na $setWindowFields partição de estágio .
O valor $setWindowFields do campo sortBy do estágio determina a ordem do documento. Para obter mais informações sobre como o MongoDB compara campos com tipos diferentes, consulte Ordem de comparação de BSON.
$shift is only available in the $setWindowFields stage.
$shift sintaxe:
{ $shift: { output: <output expression>, by: <integer>, default: <default expression> } }
$shift pega um documento com estes campos:
Campo | Descrição |
|---|---|
Especifica uma expressão para avaliar e retornar na saída. | |
Especifica um Por exemplo:
| |
Especifica uma expressão padrão opcional para avaliar se a posição do documento está fora da The default expression must evaluate to a constant value. If you do not specify a default expression, |
Comportamento
$shift returns an error if you specify a window in the $setWindowFields stage.
Exemplos
Crie uma collection cakeSales que contenha vendas de bolo nos estados da Califórnia (CA) e de Washington (WA):
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 } ] )
A coleção cakeSales é utilizada nos seguintes exemplos.
Deslocar Usando um Inteiro Positivo
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" } } } } } ] )
No exemplo:
partitionBy: "$state"particiona os documentos na collection porstate. Existem partições paraCAeWA.sortBy: { quantity: -1 }ordena os documentos em cada partição porquantityem ordem decrescente (-1), de modo que oquantitymais alto seja o primeiro.
outputdepois desortBy:Define o campo
shiftQuantityForStatepara o valorquantitydos documentos em cadastate.Uses
$shiftto return thequantityvalue from the document that follows the current document in the output.
Neste exemplo de resultado, o valor quantity deslocado é mostrado no campo shiftQuantityForState para cada documento retornado:
{ "_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" }
Deslocar Usando um Número Inteiro Negativo
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" } } } } } ] )
No exemplo:
partitionBy: "$state"particiona os documentos na collection porstate. Existem partições paraCAeWA.sortBy: { quantity: -1 }ordena os documentos em cada partição porquantityem ordem decrescente (-1), de modo que oquantitymais alto seja o primeiro.
outputdepois desortBy:Define o campo
shiftQuantityForStatepara o valorquantitydos documentos em cadastate.Uses
$shiftto return thequantityvalue from the document before the current document in the output.
Neste exemplo de resultado, o valor quantity deslocado é mostrado no campo shiftQuantityForState para cada documento retornado:
{ "_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 }