brahmana
未読、2010/12/31 17:37:082010/12/31投稿者に返信する場合はログインしてください
転送する場合はログインしてください
このグループ内のメッセージを削除する権限がありません
メールアドレスがこのグループでは匿名化されているか、メンバーのメールアドレスを表示する権限がないため、元のメッセージを表示できません
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