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

all_in/any_in criteria on an embedded document's array field

    • Type: Icon: Task Task
    • Resolution: Done
    • 2.4.3
    • Affects Version/s: None
    • Component/s: None
    • Labels:

      Given a set of class definitions and sample data like the following:

      
      class Foo
        include Mongoid::Document
        field :widgets, type: Array, default: []
        embeds_many :bars
      end
      
      class Bar
        include Mongoid::Document
        field :widgets, type: Array, default: []
        embedded_in :foo
      end
      
      Foo.create(widgets: ["foo", "bar", "baz"])
      Foo.create(widgets: ["bar", "baz", "qux"])
      Foo.create(widgets: ["baz", "qux", "png"])
      
      foo = Foo.first
      foo.bars.create(widgets: ["foo", "bar", "baz"])
      foo.bars.create(widgets: ["bar", "baz", "qux"])
      foo.bars.create(widgets: ["baz", "qux", "png"])
      

      I get wonky results when performing either an "all_in" or an "any_in" when matching widgets against an array of regexs: notably, matching the widgets coming from Foo works in a way that seems sensible, while matching widgets coming from Bar does not. For example:

      Foo.all_in(widgets: [/foo/i]).count == 1
      Foo.all_in(widgets: [/bar/i]).count == 2
      Foo.all_in(widgets: [/baz/i]).count == 3
      
      foo.bars.all_in(widgets: [/foo/i]).count == 1 # returns false; count == 0
      foo.bars.all_in(widgets: [/bar/i]).count == 2 # returns false; count == 0
      foo.bars.all_in(widgets: [/baz/i]).count == 3 # returns false; count == 0
      

      To clarify: all_in seems to allow me to find all instances of a (top-level) document which have an array field whose entries match a set of regexs. (This seems like the correct behavior to me.) However, performing the same type of search on an embedded document does not seem to behave in the same fashion. (While I've only given examples for all_in, the same difference in behavior is also exhibited for any_in.)

      Am I missing something about all_in/any_in? Am I misusing or abusing them in some nefarious way?

      Tested in version 2.4.1 in ruby 1.9.2-p290.

            Assignee:
            Unassigned Unassigned
            Reporter:
            joshuabowers joshuabowers
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved: