I am using STudio 3T and I have query like this:
select [Dashbo],lead([Dashbo]) over(order by [Entered Date])
from ATest_prevback;
This is giving me and error. How to perform this in MongoDB? Can someone give me an example?
Thanks, Adi
I am using STudio 3T and I have query like this:
select [Dashbo],lead([Dashbo]) over(order by [Entered Date])
from ATest_prevback;
This is giving me and error. How to perform this in MongoDB? Can someone give me an example?
Thanks, Adi
Starting from MongoDB v5.0+, it can be done by using $shift in $setWindowFields.
db.collection.aggregate([
{
"$setWindowFields": {
"partitionBy": null,
"sortBy": {
"entered_date": 1
},
"output": {
lag: {
$shift: {
output: "$Dashbo",
by: -1,
default: "Not available"
}
},
lead: {
$shift: {
output: "$Dashbo",
by: 1,
default: "Not available"
}
}
}
}
}
])
Here is the Mongo playground for your reference.
Unfortunately, not possible.
Workaround: Create a Materialized view with calculated data.
Run in the MongoDB Shell:
var prev = null;
db.ATest_prevback.find({}).sort({field:1}).forEach(function(curr){
db.materialized_view.insert({curr:curr.field, prev:(prev ? prev.field : 0)})
prev = curr;
})
Run "fake" lead
and lag
queries:
db.materialized_view.aggregate([
{$match:{$expr:{$eq:["$curr", "$prev"]}}}
])