3

I want to insert a document into a MongoDB in an idempotent way.

The explanation provided by the MongoDB documentation, and also here on SO, is to use the upsert=True modifier.

However, to my understanding, this does not guarantee idempotence, because an already existing document could be modified.

The operation I am looking for is as follows:

  • If the document, identified by some key, does not exist, insert it.
  • If a document with the given key already exists, there are two things that might happen:
    • The existing document and the provided document are exactly the same. Then, return the same result as if a new document would have been inserted.
    • The existing document differs from the provided document: Throw an error, because idempotence would be violated.

Does MongoDB support such an operation out of the box? Why are upsert operations labelled idempotent even though they might modify the document that is already present?

Ulrich Schuster
  • 1,670
  • 15
  • 24

1 Answers1

4

Idempotent means that you can execute the same action twice and have the same effect as if it only had happened once. The name upsert is also a hint: it is a contraction of update and insert. This means that it will either (if it exists) update an existing document, or insert a new one. So: upsert operations are labelled idempotent because they are. Your interpretation of the word idempotent is not what everyone else uses.

Your use case resp. requirement is something else. In my understanding, it is quite hard to do that in MongoDB, because there are no usable general inbuilt transaction mechanisms (at least last I looked). You'd need to do something like a two-phase commit, from your application.

Svante
  • 50,694
  • 11
  • 78
  • 122
  • 1
    My personal opinion: use a real DB instead. – Svante Sep 20 '20 at 14:53
  • 1
    Thanks for pointing out my flawed interpretation of idempotence! I found the W3C definition: "Methods can also have the property of "idempotence" in that (aside from error or expiration issues) the side-effects of N > 0 identical requests is the same as for a single request." So, updating a document is ok, because replaying the update would not introduce new side effects. – Ulrich Schuster Sep 20 '20 at 14:58
  • MongoDB supports transactions since version 4.0, which was released in June 2018. – pepkin88 May 07 '23 at 12:41