Une approche est celle-ci :
db.collection.aggregate([
{
$set: {
data: {
$let: {
vars: {
val: "$data_start",
prev: { $concatArrays: [[null], "$data_start"] } // shift values by one element
},
in: {
$map: {
input: { $range: [0, { $size: "$$val" }] },
as: "idx",
in: {
$cond: {
if: {
$and: [
{ $eq: [{ $arrayElemAt: ["$$val", "$$idx"] }, 0] }, // is 0
{ $eq: [{ $arrayElemAt: ["$$val", "$$idx"] }, { $arrayElemAt: ["$$prev", "$$idx"] }] } // is equal to previous
]
},
then: null,
else: {
data_start: { $arrayElemAt: ["$$val", "$$idx"] },
time_start: { $arrayElemAt: ["$time_start", "$$idx"] }
}
}
}
}
}
}
}
}
},
{
$set: {
data: {
$filter: {
input: "$data",
cond: "$$this"// -> removes null's from array
}
}
}
}
])
Peut-être que vous devez affiner la condition et/ou inverser la boucle, c'est-à-dire input: { $range: [{ $size: "$$val" }, 0, -1] }