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
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:
Is it possible to do this with multiple documents like insertMany?
Quite succinctly from the above link:
When you specify the option upsert: true:
- If document(s) match the query criteria,
db.collection.update()performs an update.- If no document matches the query criteria,
db.collection.update()inserts a single document.
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: