I have an array of time periods on a mongo document.
it looks something like this
{
spans: [{from:2016-1-1, to:2016-1-4},
{from:2016-2-1, from:2016-2-4}]
}
I want to spit the first item into two (eg modify the existing element and add a new one) so that it looks like this
{
spans: [{from:2016-1-1, to:2016-1-2},
{from:2016-1-3, to:2016-1-4},
{from:2016-2-1, from:2016-2-4}]
}
How can I do this in a single operation (I want it to succeed or fail together)?
I have tried doing an addToSet
and set
on the element but I cant as I get :
Cannot update 'spans' and 'spans.1.from' at the same time
I could do this by replacing the whole array at once but that seems extremely inefficient. I was wondering if there is a better way.