I'm using a nodejs client to loop over input and create an array of values that will be handed off to the bulkWrite
operation:
var updateVal = { sku: item.sku, name: item.name, updatedAt: new Date()};
var insertVal = { sku: item.sku, name: item.name, createdAt: new Date(), updatedAt: new Date()};
itemsToSave.push({
updateOne: {
filter: {
sku: item.sku
},
//update: { $set: updateVal }, // works
//update: { $setOnInsert: insertVal }, // works
update: { $set: updateVal, $setOnInsert: insertVal }, // DOES NOT work
upsert:true
}
});
But for some reason I can't mix $set
and $setOnInsert
... when I run it, nothing happens, it is like a no-op. Is this a limitation of bulk operation?
I'm using node-mongodb-native/2.1 to work with mongodb 3.0.7
The docs for setOnInsert suggest that this should be possible:
...
{
$set: { item: "apple" },
$setOnInsert: { defaultQty: 100 }
},
{ upsert: true }
That's why my question is about using it with bulkWrite
Update
This worked:
update: {
$currentDate: { updatedAt: true },
$set: updateVal,
$setOnInsert: {
createdAt: new Date()
}
},
upsert:true
thanks to the sample found via github code search: https://github.com/rainder/node-process-stat/blob/22899f49413314402645ae57bc691bd389d226bd/src/server/router/log.js#L49-L59