I want to check if let's say 2000 URLs exist in a collection and insert the ones that are not already existing along with some other fields as a new document. I need to update only the timestamp field for the ones that are already exist. Usually new ones will be way less.
Currently I loop in async.each() and do an upsert for each and use updatedExisting field in the response to see if the URL is new. But maybe I should do findOne and insert or update. Another option is do a bulk find and do update for existing ones in async.each and do bulk insert for the new ones. Please advice! Thanks.
OK I think upsert would work well in your situation have a look at this page: Mongo Bulk Upsert
and this is a simple example of how it could work:
bulk.find( { item: "abc123" } ).upsert().replaceOne(
{
item: "abc123",
status: "P",
points: 100,
}
);
bulk.execute();
I hope that helps.