Update in bulk, insert if record missing

The following records are inserted in a collection.
[{"_id" : 1, “field1” : “abc”}, {"_id" : 2, “field1” : “xyz”}]

I want to process the following list in such a way that if the _id already exists, the record is updated else inserted as a fresh record.

[{"_id" : 1, “field2” : “pqr”}, {"_id" : 3, “field2” : “tpr”}]

I could do this in a loop using update, but is there a way to do this in bulk?


I have found a way which seems working fine. Posting here for the benefit of others. Although this is still not SQL equivalent of join and update, and still needed updates for each record individually, the performance was very good.

update_list = []
for val in values:
    update_list.append(UpdateOne({'_id': val['_id']}, {'$set': {'xyz': val['xyz']}}, upsert=True))

Essentially, we have to build a list of individual UpdateOne commands, and execute in bulk using bulkwrite (I was using Python in this example).

     upsert: true

see this for more information