Search code examples
mongodbmongoosenestjsbulk

Bulk upsert optimization mongoDB


I have a collection with about 50 000 entries which contains some fields.

The combination of two of them is uniq (an ObjectID and a string).

I takes about 15s to upsert 500 entries. Is there a way to get it done faster ? I tried to use bulkWrite (with ordered:false) instead of multiple updateOne() on collection with nestJS but there is no gain.

myObjects.forEach(objects => {
const upsertDoc = {
      updateOne: {
           filter: { someOid: myOid, someString: myString },
           update: { $set: myObject },
           upsert: true,
      },
};
bulkOps.push(upsertDoc);
}

myModel.collection.bulkWrite(bulkOps, { ordered: false });

I tried with an empty collection and it's a way faster (200ms), I guess that's the filter part that take too long.


Solution

  • The solution I found was to search not over two field, but only one (simply the _id in ma case), this drastically improve performance