I would expect this:
class Doc include Mongoid::Document field :some_hash, type: Hash end doc = Doc.where(some_hash: { foo: :bar }).first_or_create doc.some_hash
to be {{
{ foo: :bar }}} but in actuality it is nil.
As it turns out, Criteria::Modifiable#create_document ignores Hashes completely. I believe this "feature" originated from the need to ignore complex conditions such as gt as evidenced by the comments from way back in 2010.
In my opinion, it is a legitimate use case to e.g. first_or_create, build, … on criteria including Hashes but I'm not sure how much effort would be required to support that use case without introducing unintended side-effects. I am creating this issue to solicit comments and/or get a definitive WONTFIX decision. In the latter case, I think the documentation should be updated to point out the aforementioned restriction.
- related to
-
MONGOID-4261 .find_or_create and similar methods lead to data corruption or crash when the query contains special operators (such as $in)
- Closed