Is it possible to solve the following problem with a SINGLE QUERY?
Basically I’m looking for a solution to solve the following problem. Since there’re hundreds of such operations at each moment, the operation needs to be FAST.
So I have a curves collection with documents like this:
{
curveId: 12,
values: [1, 2, 12, 7, ...]
}
Suppose I’d like to set a curve value at index 3 and result in:
{
curveId: 12,
values: [1, 2, 12, new_value, ...]
}
in case of no matched curveId, a new curve is created:
{
curveId: 12,
values: [null, null, null, new_value]
}
so I write this upsert query:
db.curves.update({
curveId: 12
},{
$set: { "values.3": new_value }
},{
upsert: true
})
This works when there’s a matched document. However, if there’s no match, it will create a new element like this:
{
curveId: 12,
values: { '3': new_value }
}
Not what I was expecting.
I’ve googled quite some time but found no solution yet. Is it even possible to solve the problem with one query?
Thank you.
3