Tolga Çelik
Oct 25, 2021

--

MongoDB Upsert operation with updateMany

Add specific column updateMany ,upsert

try {
db.items.updateMany(
{},
{ $set: { “settings.value”: true } },
{ upsert: true }
);
} catch (err) {
print(err.name + ‘: “‘ + err.message + ‘“.’);
}
— — — — — or with bulk update — — — — — — — — — — -var bulk = db.items.initializeUnorderedBulkOp();
bulk.find({}).upsert().update({
$set: {
“settings.value”: true
}
} );
bulk.execute();

however, i havent find the solution for shared key issue.

WriteError: “An upsert on a sharded collection must contain the shard key and have the simple collation. Update request: { q: {}, u: { $set: { settings.value: true } }, multi: true, upsert: true }, shard key pattern: { _id: “hashed” }”.

--

--