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

Pipelines on associations don't retain _type, producing wrong results

    • Type: Icon: Bug Bug
    • Resolution: Works as Designed
    • Priority: Icon: Major - P3 Major - P3
    • None
    • Affects Version/s: 8.1.1
    • Component/s: None
    • None

      It looks like there's a bug in how `_type` gets carried through, in my particular case when chaining on an association. The result is that all _types are returned in results, instead of the single type one would expect (from the association).

      Test program demonstrating the problem:

      require 'mongoid'
      Mongoid.configure ...
      
      class Graph
      include Mongoid::Document
      has_many :people
      has_many :organizations
      end
      
      class Entity
      include Mongoid::Document
      field :key, type: String
      belongs_to :graph
      end
      
      class Person < Entity
       # Optional doesn't matter I promise, it's just to make for less typing
      belongs_to :organization, optional: true
      end
      
      class Organization < Entity
      has_many :people
      end
       # Just helpful shorthand
      class Mongoid::Criteria
      def aggregate
      collection.aggregate pipeline
      end
      end
      
      Graph.destroy_all
      Entity.destroy_all
      
      g = Graph.create
      o1 = Organization.create key: "first organization", graph: g
      o2 = Organization.create key: "second organization", graph: g
      p1 = Person.create key: "first person", graph: g # organization: o1
      p2 = Person.create key: "second person", graph: g #, organization: o2
      
      puts g.people.aggregate.count # => 4 - WRONG
      puts g.people.project(id: 1, _type: 1).aggregate.count # => 4 - WRONG
      puts g.people.count # => 2 - CORRECT!
      puts g.people.project(id: 1, _type: 1).aggregate.count # => 2 - NOW IT'S CORRECT!
      puts g.people.aggregate.count # => 4 - STILL WRONG THO
      

            Assignee:
            jamis.buck@mongodb.com Jamis Buck
            Reporter:
            jpr5 Jordan Ritter
            Votes:
            0 Vote for this issue
            Watchers:
            4 Start watching this issue

              Created:
              Updated:
              Resolved: