brahmana
unread,Dec 31, 2010, 5:37:08 PM12/31/10Sign in to reply to author
Sign in to forward
You do not have permission to delete messages in this group
Either email addresses are anonymous for this group or you need the view member email addresses permission to view the original message
to Mongoid
Hi all,
First of all, Happy new year. :)
The question now :
Is the Inc modifier in Mongoid really atomic? Because it uses
write_attribute which marks the attribute being incremented as dirty
and will included for a "$set" again later when the document is saved.
Class Post
include Mongoid::Document
field :author
field :views, :type => Integer
...........
...........
...........
end
p = Post.first(:conditions => {_id : <some_id>})
p.inc(:views)
# Results in an update query to mongodb -
blogdb['posts'].update({_id : <some_id>}, {"$inc" : {views : 1}})
# view is marked dirty
# Do some more updates to p and save it
p.save
# Results in an update query to mongodb with "$set" on the "views"
field too (assume previous value of "views" was 0)
# blogdb['posts'].update({_id : <some_id>}, {"$set" : {views : 1}})
The second operation ("$set") effectively overrides the first ("$inc")
there by making the atomic increment sort of useless. Also, more
importantly if there was another increment from another Rails instance
(another app-server process may be) before this save, the second
increment is lost.
Shouldn't the Inc modifier directly update the @attributes hash on the
document instead of using write_attribute?
This way the field is not marked dirty.
Thoughts?
Regards,
Brahmana