I have a document with an order array that contains ids.
Sometimes I want to set specific indexes in that array:
db.getCollection('myCollection').update(
{
_id: 'someId',
},
{
$set: {
'order.1': 'foo2',
},
},
);
This works correcly and updates the second array element. If the array is empty, the 0th element is null.
Trying the same with bulkWrite leads to really weird behavior:
db.getCollection('myCollection').bulkWrite([
{
updateOne: {
filter: { _id: 'eager' },
update: [
{
$set: {
'order.1': 'foo3',
},
},
],
},
},
]);
If the array in the db is empty this does nothing.
However, if there are elements in the array, this replaces every array element with an object of the form { '1': 'foo3' }
.
Does anyone have any idea why this happens and how to make bulkWrite behave? 😀