Database
[{"clientNo":3,"activeSubNo":0,"upcomingSubNo":3,"subscriptions":[{"subNo":1,"Type":"FREE","startDate":ISODate("2021-09-02"),"endDate":ISODate("2021-11-02")},{"subNo":2,"Type":"PAID","startDate":ISODate("2021-12-02"),"endDate":ISODate("2022-02-04")},{"subNo":3,"Type":"PAID","startDate":ISODate("2022-02-05"),"endDate":ISODate("2022-05-04")}]},{"clientNo":5,"activeSubNo":2,"upcomingSubNo":0,"subscriptions":[{"subNo":1,"Type":"FREE","startDate":ISODate("2021-09-02"),"endDate":ISODate("2021-11-02")},{"subNo":2,"Type":"PAID","startDate":ISODate("2021-12-02"),"endDate":ISODate("2022-02-03")}]},{"clientNo":6,"activeSubNo":1,"upcomingSubNo":0,"subscriptions":[{"subNo":1,"Type":"FREE","startDate":ISODate("2022-01-01"),"endDate":ISODate("2022-02-28")}]}]
Query
db.collection.aggregate([{$set:{subscriptions:{$cond:{if:{$eq:["$upcomingSubNo",0]},then:"$$REMOVE",else:{$arrayElemAt:["$subscriptions",{$subtract:["$upcomingSubNo",1]}]}}},startDate:{$arrayElemAt:["$subscriptions.startDate",{$subtract:["$activeSubNo",1]}]},endDate:{$arrayElemAt:["$subscriptions.endDate",{$subtract:["$activeSubNo",1]}]}}}])