1

My question is clear as in the title. When a request come to my service for updating related record in mongoDb, we use "save" method.

However, I would like to understand whether the save method really updates the record or not.

In other words, I would like to know if the content going to save is the same with the existing content in mongoDb. Accordingly, even if save method is executed without any errors, is it possible to understand whether it is really updated or not?

Thanks in advance

5
  • What server-side language and driver are you using?
    – Sammaye
    Nov 29, 2012 at 9:28
  • why do you want to know this? As my answer says - there is no way to know if the old and new docs were the same without comparisons of documents. Regardless of whether they were or not, the write always happens. Nov 30, 2012 at 3:58
  • Oh, in that case, you need to watch this, ... youtube.com/watch?v=b2F-DItXtZs
    – raffian
    Nov 30, 2012 at 14:24
  • @SAFX how does that help here?
    – Sammaye
    Nov 30, 2012 at 22:48
  • I would like to know this because I do not want to do any additional lots of work and looking for the most convenient and efficient way to find out whether the data being updated is identically same with the existing one. Is there any clue that helps me to figure out that, say, like a method getLastError() or something else? Thanks in advance.
    – javatar
    Dec 3, 2012 at 0:01

2 Answers 2

1

There are several ways to checks this.

The first is after calling Save, is to call the getLastError method. Within the console this is just db.getLastError().

This will tell you if an error occurred during the last operation. More details can be found at te following address http://docs.mongodb.org/manual/core/write-operations/#write-concern.

Another way would be to call findAndModify, this will allow you to update the document and either get the updated document back. http://docs.mongodb.org/manual/reference/command/findAndModify/

Both of these are available in all of the official drivers.

3
  • this is not going to tell him whether the update was unnecessary. he's not asking whether the update happened, it sounds like he wants to know whether it introduced any new data... Nov 30, 2012 at 3:55
  • @AsyaKamsky Using findAndModify to return the original document would allow them to see if the document was updated very easily.
    – SCB
    Nov 30, 2012 at 4:41
  • 1
    as long as they keep in mind that physically the write happened regardless - they can check if it was a useless write by comparing previous and new version of the document. Nov 30, 2012 at 15:36
1

Save method always writes the record.

There is no situation in Mongo where the write would not happen because the record that is being saved is identical to the record that's already there. The write would simply happen and "overwrite" existing data with new data (which happens to be identical).

The only way you can tell is by comparing the old and new documents - and that's a lot of extra work.

2
  • Thanks for respnnse. Actually I would like to find out which approach is the efficient way to understand whether to data being updated is identically same with the existing one AFTER write operation. Is there any method (like getLastError method assuming it may give me the comparison or any other clue).
    – javatar
    Dec 2, 2012 at 23:59
  • it could be findAndModify - it returns back the previous (now overwritten) version of the document by default. You can then compare it to document you just updated it to. Dec 3, 2012 at 7:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.