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

Adding .sort(...) to an $or query prevents use of indexes.

    • Type: Icon: Bug Bug
    • Resolution: Duplicate
    • Priority: Icon: Major - P3 Major - P3
    • None
    • Affects Version/s: 2.0.1
    • Component/s: Index Maintenance
    • ALL

      This was generated from the Google Groups discussion, "sorting, does not use defined index."
      http://groups.google.com/group/mongodb-user/browse_thread/thread/9bc121708a0a5cde

      To reproduce:
      > db.example.drop();
      > db.example.save({_id:1, "a":"aaa"});
      > db.example.save({_id:2, "a":"bbb"});
      > db.example.save({_id:3, "a":"ccc"});
      > db.example.save({_id:4, "a":"ddd"});
      > db.example.save({_id:5, "a":"eee"});
      > db.example.save({_id:6, "a":"fff"});
      > db.example.save({_id:7, "a":"ggg"});
      > db.example.save({_id:8, "a":"hhh"});
      > db.example.ensureIndex(

      {a:1}

      );

      > db.example.find({$or:[

      {a:"aaa"}

      ,

      {a:"bbb"}

      ]}).explain(true)

      The index is used, and "nscanned" : 2

      Now sort the results:

      > db.example.find({$or:[

      {a:"aaa"}

      ,

      {a:"bbb"}

      ]}).sort(

      {a:1}

      ).explain(true)
      The index is still used, but "nscanned" is now 8, indicating that Mongo is accessing every document in the collection.

            Assignee:
            Unassigned Unassigned
            Reporter:
            mbastien Marc Bastien
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved: