"if document is in collection update it, if not insert the document" query

Is there a query like: if document is in collection update it, if not insert the document

I want to do this with multiple documents in one query like insertMany

Hi @mental_N_A

This is called an upsert:

https://docs.mongodb.com/manual/reference/method/db.collection.update/#insert-a-new-document-if-no-match-exists-upsert

1 Like

Is it possible to do this with multiple documents like insertMany?

Quite succinctly from the above link:

When you specify the option upsert: true:

I want to do it like Query([document1, document2, document3]). It checks if each document is in the collection, if it’s not in the collection it inserts it, if it is then it’ll update it.

Try with the following:
https://docs.mongodb.com/manual/reference/method/db.collection.bulkWrite/

3 Likes