-1

I have around 400 fields in my collection (including both at top level as well as embedded), following is the nature of write queries:

  1. All write queries always update single document and an average of 60 fields in that document.
  2. There are indexed fields in collection but no write query updates an indexed field.
  3. Volume of write queries is very large.

I can use either .save() or .update() to update the document. In update I only pass the fields that need to be updated, whereas in save I pass the entire document. I want to know if using update in this case will give me better performance than save (or vice versa) or does it not make any difference at the database level and both perform equally well?

Punit Goel
  • 59
  • 6
  • Yes the linked duplicate was written in response to "mongoose" API usage, but exactly the same rules apply to the spring-data implementation as well. The **main** distinction on performance depends more on whether you **A.** Fetch data, then make modifications and finally `save()` back. Or **B.** Simply issue a an `update()` with only the changes to be made back to the server for the matching document(s). It's actually the **fetching** pattern that makes the real difference as opposed to any actual difference in the `save()` or `update()` methods themselves. MongoDB itself only has `update()`. – Neil Lunn Apr 14 '19 at 09:28
  • @NeilLunn In my case I had to fetch the document first to actually decide how and what to update, So as I got from your answer both save() and update() will perform equally well and it doesnot depend on document size(fields) and fields to be updated..right? – Punit Goel Apr 14 '19 at 11:56
  • 1
    What you *"should"* have learned from that is that "fetch and save" is a *horrible* pattern and I cannot discourage it's usage enough. An `update()` is sending a note to your publisher to "edit" content of one page in a book. A `save()` is where you take a trip to the publisher, take the book home, type out a new page and insert it and then take the trip back to the publisher to give them the book back. It should stand out which of those is actually more efficient. – Neil Lunn Apr 14 '19 at 12:03
  • I understood that but it depends on the use case whether fetching the document first is needed or not, my question was primarily about the DB level impact if any – Punit Goel Apr 14 '19 at 12:15

1 Answers1

1

It doesn't make any significant change in performance. The reasons are as below

When you save or update a document in mongodb, you probably decide to call save or update from another application that could be written in C#, Java, JavaScript, PHP or someother language.

In this case, there is a Inter process communication (or network call if you mongo db is running in another machine). When compared to this the difference in time take to selectively replace a document by update and completely replace the document by save is negligible. By the way, save and update both will probably have run time complexity of O(n) if there is no indexes.

For a document with 250 fields, the size of the document is probably not too big that we have to consider. If the size of the update document is significantly smaller that size of the save document, then please use update.

Else use a save/update depending on the which is more elegant in the client side code.

  • "When compared to this the difference in ... by save is negligible." "If the size of the update document ..., then please use update." So what's the conclusion? – Rainning Mar 27 '21 at 15:44