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

Query find_by adds a sort _id

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

      I've noticed mongoid automatically adds a $orderby"=>{:_id=>1} to find_by query.

      For example, User.find_by(:email => 'some@email.com') does the following query
      users selector={"$query"=>

      {"email"=>"kirk@grantoo.org"}

      , "$orderby"=>{:_id=>1}} flags=[] limit=-1 skip=0 batch_size=nil fields=nil runtime: 0.8535ms

      This is pretty taxing on a large production database. Because of the extra sort, there would need to be a multi-key index of {:email => 1, :_id => 1} instead of just {:email => 1}.

      In our case, that's an extra 800MB of ram required to store the extra :_id sorting.

      The current workaround I have is to specify a mock sort. For example, I'd do User.order_by(:email.asc).find_by(:email => 'some@email.com')

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

              Created:
              Updated:
              Resolved: