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

#max on Mongoid::Criteria incorrectly changes selector

    • Type: Icon: Task Task
    • Resolution: Done
    • Priority: Icon: Major - P3 Major - P3
    • 5.0.1
    • Affects Version/s: None
    • Component/s: None
    • None

      I'm trying to get the max on a field over a Criteria, chaining after a #where method. This is definitely expected to work, and is a featured example in the docs.

      The query in question (I tried this both with and without regex, and it behaves incorrectly both ways):

      account.campaigns.where(name: /Untitled (\d+)/).max(:name)
      

      Without the #max, it properly generates the selector:

      MOPED: 127.0.0.1:27017 QUERY        database=development collection=
      campaigns selector={"account_id"=><BSON::ObjectId:0x70215006872120 data=55831dc2636173383a000000>, "name"=>/Untitled Campaign (\d+)/} flags=[] limit=0 skip=0 batch_size=nil fields=nil runtime: 0.3168ms
      

      With the #max, it changes the selector to an empty $nin selector:

      MOPED: 127.0.0.1:27017 COMMAND      database=development command={:a
      ggregate=>"campaigns", :pipeline=>[{"$match"=>{"account_id"=><BSON::ObjectId:0x70215006872120 data=55831dc2636173383a000000>, "name"=>{"$nin"=>[]}}}, {"$group"=>{"_id"=>"name", "count"=>{"$sum"=>1}, "max"=>{"$max"=>"$name"}, "min"=>{"$min"=>"$name"}, "sum"=>{"$sum"=>"$name"}, "avg"=>{"$avg"=>"$name"}}}]} runtime: 0.3208ms
      

      Instead of returning the max of documents that match the #where condition, it just returns the max for the name field across all documents matching this account_id. I suspect this may have something to do with querying on the same field that I'm attempting to get the max of.

      edit: This is with mongoid 4.0.2, moped 2.0.6, origin 2.1.1.

            Assignee:
            emily.stolfo Emily Stolfo
            Reporter:
            danarnold danarnold
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

              Created:
              Updated:
              Resolved: