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

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

    • Type: Icon: Bug Bug
    • Resolution: Duplicate
    • Priority: Icon: Critical - P2 Critical - P2
    • None
    • Affects Version/s: 2.6.5
    • Component/s: Index Maintenance, Querying
    • None
    • ALL

      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

            Assignee:
            david.storch@mongodb.com David Storch
            Reporter:
            rpechayr Romain Pechayre
            Votes:
            1 Vote for this issue
            Watchers:
            7 Start watching this issue

              Created:
              Updated:
              Resolved: