Uploaded image for project: 'Mongoid'
  1. Mongoid
  2. MONGOID-4261

.find_or_create and similar methods lead to data corruption or crash when the query contains special operators (such as $in)

    • Type: Icon: Bug Bug
    • Resolution: Done
    • Priority: Icon: Critical - P2 Critical - P2
    • 6.0.0
    • Affects Version/s: 5.1.2
    • Component/s: None
    • Labels:
      None

      emily.stolfo, I appreciate you fixing https://jira.mongodb.org/browse/MONGOID-3887.

      Unfortunately, the approach as released in 5.1.2 breaks down when the query scope contains special operators such as Person.in(aliases: "foobar").first_or_create.

      Mongoid would attempt to set the aliases field to {{ "$in" => ["foobar"] }} which of course is undesired. I have created to demonstrate the problem.

      I am not sure what the best fix would be. I would suggest to either roll back MONGOID-3887 entirely – mongoid would then not support taking over Hash fields from the scope when using e.g. .first_or_create – or detecting special operators and ignoring them when dumping values into the new document. The latter would require great care to implement such that the issue doesn't recur when using nested operators or other oddities.

      One could also merely document that fact that .first_or_create et al don't work as expected when confronted with special operators in the where clause but I would personally be against that "solution". Scopes might be passed around a lot before finally a .first_or_create gets appended and it would be difficult to ensure that the scope and final find or create call are compatible as they might be located in totally different parts of a codebase with the scope author not having taken into account that eventually a .first_or_create might be called on his scope.

      Thoughts? I could take a stab at either solution if you were interested.

            Assignee:
            emily.stolfo Emily Stolfo
            Reporter:
            niels@herimedia.com Niels Ganser
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

              Created:
              Updated:
              Resolved: