I'm trying to insertMany() items into my Mongo database but I would like to skip duplicate IDs.
I'm using Node.js and mongodb.
I have some data:
const myExampleData = [
{_id:'someId1', name:'I am example'},
{_id:'someId2', name:'I am second example'}
];
and I would like to insert them like this:
dbo.collection(collectionName).insertMany(myExampleData).catch(err=>{
console.error(err);
});
Lets suppose that someId1 already exists. I don't want to override it. I just want to skip it. In current situation it doesn't insert someId2. It stops as soon as it throws duplicate exception.
Is there a way how to insertMany and skip duplicates?
Possible question duplicate.
I've found thread MongoDB insert without duplicates where is suggested to use update() with upsert instead of insert() which could be fine for one item. But how about many items? As far as I know updateMany() would update all filtered rows with the same value but I would like to insert different values.
所有评论(0)