I have a collection people. It has an array field numbers, where each document has a varying number of elements in said array.
My goal is to keep the elements in the 0th index and recursively remove the rest. How could I go about doing this?
For example:
{_id: 1, numbers: [100, 200, 300]}, -> {_id: 1, numbers: [100]},
{_id: 2, numbers: [101, 201]}, -> {_id: 2, numbers: [101]}
{_id: 3, numbers: [102, 202, 400, 500]}, -> {_id: 3, numbers: [102]},
{ $push: { numbers: { $each: [], $slice: 1 } } }could work. $slice: -1 would keep only the last element.