Update vs save

221 views
Skip to first unread message

nilskp

unread,
Oct 29, 2012, 11:42:49 AM10/29/12
to mongod...@googlegroups.com
I have a document with two fields, the _id and another field that needs constant updating. I have two choices on how to update that field.
  1. Keep the object around in memory, change the field and call save.
  2. Call update with $set on the field.
Is there any reason to think that one would be faster than the other?

Sam Millman

unread,
Oct 29, 2012, 12:30:15 PM10/29/12
to mongod...@googlegroups.com
I suppose not though $set has an atomic lock rather than the save which would take write lock (it just resaves the whole document), with constant updating an atomic lock might be better here.


--
You received this message because you are subscribed to the Google
Groups "mongodb-user" group.
To post to this group, send email to mongod...@googlegroups.com
To unsubscribe from this group, send email to
mongodb-user...@googlegroups.com
See also the IRC channel -- freenode.net#mongodb

Russell Smith

unread,
Oct 29, 2012, 12:46:38 PM10/29/12
to mongod...@googlegroups.com
Is the value changing length? If so you might want to think about padding the document so you don't end up fragmenting things.

Russ
Rainforest | +1-650-919-3216 | rainforestqa.com

Randolph Tan

unread,
Nov 1, 2012, 11:11:55 AM11/1/12
to mongod...@googlegroups.com
In theory, $set should be faster as you are sending fewer bytes across the wire, but the difference can be negligible is the document is small. So it really depends on your use case. I would encourage you to actually benchmark and see if the difference matters for your case.

Nils Kilden-Pedersen

unread,
Nov 1, 2012, 2:36:26 PM11/1/12
to mongod...@googlegroups.com
Why fewer bytes?


--

Randolph Tan

unread,
Nov 1, 2012, 4:11:53 PM11/1/12
to mongod...@googlegroups.com
Because with save, you are sending the entire document, even the parts that where unchanged, while for $set, you are only sending the parts that you are setting.

Nils Kilden-Pedersen

unread,
Nov 1, 2012, 9:18:57 PM11/1/12
to mongod...@googlegroups.com
Please read my original post.

Randolph Tan

unread,
Nov 2, 2012, 10:31:51 AM11/2/12
to mongod...@googlegroups.com
Sorry, I missed the 2 fields part. In that case, save is theoretically faster because all the server does is get the old record, check if the new object still fits the record and just smacks it in. While for the $set case, it has to get the old record and rebuild a new one with the new field. As always, I would encourage you to perform benchmarks so you would be more convinced of which one fits your requirements.

Nils Kilden-Pedersen

unread,
Nov 2, 2012, 12:04:08 PM11/2/12
to mongod...@googlegroups.com
Ok, thanks. I will test later, but I'm strapped for time right now.
Reply all
Reply to author
Forward
0 new messages