-
Type: Bug
-
Resolution: Fixed
-
Priority: Major - P3
-
Affects Version/s: 7.3.0
-
Component/s: None
-
None
-
Fully Compatible
Since https://jira.mongodb.org/browse/MONGOID-3468 .touch(:field) is broken for embedded documents. The touched timestamp is not persisted in the database for the embedded document.
This is because when a document is embedded, the persistence is delegated to the parent document, (here: https://github.com/mongodb/mongoid/blob/master/lib/mongoid/touchable.rb#L37 ) however a `field` argument is not given, and since it's nil the code that handles the atomic touch update here https://github.com/mongodb/mongoid/blob/2f3505c3641718b8fdbe2b8b81fc58dd517b29ee/lib/mongoid/atomic.rb#L384
effectively removes the atomic operation for the embedded document.
This is because the regex generated in this code does not handle the already passed update to a custom touch field.
def touch_atomic_updates(field = nil) updates = atomic_updates return {} unless atomic_updates.key?("$set") touches = {} updates["$set"].each_pair do |key, value| key_regex = /updated_at|u_at#{"|" + field if field.present?}/ touches.merge!({ key => value }) if key =~ key_regex end { "$set" => touches } end # the embedded document generated this atomic update # atomic_updates = {"$set"=>{"email_suppressions.0.notified_at"=>2010-10-10 10:00:00 UTC}} # because the parent is touched, the field is `nil` # the regex will be /updated_at|u_at/ # this does not match the key =~ key_regex # the resulting $set will thus become: `{"$set"=>{}}` # effectively removing the atomic update for an embedded document # when the field is custom
- is caused by
-
MONGOID-3468 Always touch parents of embedded documents when embedded documents are touched
- Closed
- is related to
-
MONGOID-5142 Perform a single write for all touches
- Closed
- links to