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

Document order in default scope taking precedence over order in query

    • Type: Icon: Improvement Improvement
    • Resolution: Fixed
    • Priority: Icon: Minor - P4 Minor - P4
    • 7.1.0.rc0
    • Affects Version/s: None
    • Component/s: Docs, Query

      If an order is specified in the default scope and in the query, the default scope order is added in front of the query order. The result is somewhat surprising - order in default scope takes precedence:

      class Band
        include Mongoid::Document
        field :name, type: String
        field :description, type: String
        default_scope ->{ order(description: 1) }
      end
      
      Band.create!(name: 'aaa', description: 'z')
      Band.create!(name: 'bbb', description: 'b')
      
      Band.all.to_a
      => [#<Band _id: 5de6bc3dce4ef31e5e2d1cff, name: "foo", description: nil>, #<Band _id: 5de6bc47ce4ef31e5e2d1d00, name: "bar", description: nil>, #<Band _id: 5de6bd2ace4ef31e5e2d1d02, name: "bbb", description: "b">, #<Band _id: 5de6bd35ce4ef31e5e2d1d04, name: "bbb", description: "b">, #<Band _id: 5de6bd2ace4ef31e5e2d1d01, name: "aaa", description: "z">, #<Band _id: 5de6bd35ce4ef31e5e2d1d03, name: "aaa", description: "z">]
      
      Band.order(name: 1).to_a
      => [#<Band _id: 5de6bc47ce4ef31e5e2d1d00, name: "bar", description: nil>, #<Band _id: 5de6bc3dce4ef31e5e2d1cff, name: "foo", description: nil>, #<Band _id: 5de6bd2ace4ef31e5e2d1d02, name: "bbb", description: "b">, #<Band _id: 5de6bd35ce4ef31e5e2d1d04, name: "bbb", description: "b">, #<Band _id: 5de6bd2ace4ef31e5e2d1d01, name: "aaa", description: "z">, #<Band _id: 5de6bd35ce4ef31e5e2d1d03, name: "aaa", description: "z">]
      
      Band.order(name: 1)
      => #<Mongoid::Criteria
        selector: {}
        options:  {:sort=>{"description"=>1, "name"=>1}}
        class:    Band
        embedded: false>
      
      

      This behavior matches ActiveRecord one and is this way because the default scope (with its order) is evaluated first, with query conditions being evaluated later.

      Having the query conditions take precedence is tricky because both default scope and query may have multiple order statements, thus we would need to keep track of "order insertion point". Additionally this would make Mongoid behave contrary to ActiveRecord.

      Since the current behavior can be surprising, this ticket is to document it in the tutorial.

            Assignee:
            oleg.pudeyev@mongodb.com Oleg Pudeyev (Inactive)
            Reporter:
            oleg.pudeyev@mongodb.com Oleg Pudeyev (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

              Created:
              Updated:
              Resolved: