Supposing I have the following array:
{
data: [1, 0, 4, 0, 0, 4, 1, 3, 0, 1, 0, 2, 2, 0, 1, 1, 0, 2, 0, 4, 1, 1, 0, 1, 1, 0]
}
if I want to select all the elements, except the last 3, I can use the solution proposed here
db.collection.aggregate([
{
$project: {
"_id": 0,
"data": {
"$slice": [
"$data",
0,
{
$subtract: [ { $size: "$data" }, 3 ]
}
]
}
}
}
])
I can get the desired output:
[1, 0, 4, 0, 0, 4, 1, 3, 0, 1, 0, 2, 2, 0, 1, 1, 0, 2, 0, 4, 1, 1, 0]
How can I do the same thing for concatenated/nested arrays like:
{
data: [[1, 0, 4, 0, 0, 4, 1], [1, 0, 4, 0, 1, 5, 3]]
}
In order to select all the elements, except the last 3 (of each array)?
Expected output:
{
data: [[1, 0, 4, 0], [1, 0, 4, 0]]
}