1

Quick question: In mongo ( or any db for that matter), are upsert queries a better alternative to insert + update query.

upsert -> finds and updates document if found else inserts a new document. I've read articles saying this makes life easier and is performance effective. (Essentially two operations) insert -> checks if document exists, if not -> insert document else throw an error. ( 2 operations) update would be the reverse of insert. (2 operations again )

From this upsert seems to be a better choice but what if I had data which rarely gets updated. Would having an insert & update query make more sense or an upsert operation?

Community
  • 1
  • 1
the_lost_one
  • 127
  • 10

1 Answers1

1

If you're mostly inserting new data but occasionally amongst that data there could be a document that you know you will want to update, then upsert will save you having to code extra error handling or checking routines. In that case it would be "better". It does depend on your data and how you want to handle exceptions, though. Make sure you are OK with updating documents with no warning, and that there are no cases where you want an insert to fail due to existing data.

Craig
  • 11
  • 3