Uploaded image for project: 'Core Server'
  1. Core Server
  2. SERVER-16350

Wrong index choice when request contains _id in query + sort command

    XMLWordPrintable

    Details

    • Type: Bug
    • Status: Closed
    • Priority: Critical - P2
    • Resolution: Duplicate
    • Affects Version/s: 2.6.5
    • Fix Version/s: None
    • Component/s: Indexing, Querying
    • Labels:
      None
    • Operating System:
      ALL

      Description

      Hello,

      I encountered a strange behavior in Mongo 2.6.x. i am using Mongoid in ruby talk to MongoDB and Mongoid allows having default sort command. When I run the following command :

      Person.find("someindex") in Mongoid, it is translated into
      db.persons.find(

      {deleted_at: null, _id: ObjectId("someindex")}

      ).sort(created_at: -1)

      Mongoid is not really smart by generating such query because we know we will get either 0 or 1 result, so the sort operator does not make sense.

      However, I've found cases where MongoDB is even less smart and caches the wrong index, including the created_at field.

      I would expect MongoDB to automatically use the _id index whenever it is present in the query, no matter other fields that are present in the query, in partcular sort commands.

      I already had a discussion about this on google group:
      https://groups.google.com/forum/#!searchin/mongodb-user/rpechayr/mongodb-user/-btb_43ONfk/w0dMlECPInIJ

        Attachments

          Issue Links

            Activity

              People

              • Votes:
                1 Vote for this issue
                Watchers:
                8 Start watching this issue

                Dates

                • Created:
                  Updated:
                  Resolved: