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

Using operator $in in find method. Prevents using index to sort result with limit.

    • Type: Icon: Bug Bug
    • Resolution: Works as Designed
    • Priority: Icon: Major - P3 Major - P3
    • None
    • Affects Version/s: 4.2.1
    • Component/s: Querying
    • Labels:
    • ALL
    • Query 2019-12-30

      I have a collection and compound index on it 

      IX_checkin {checkin:1, nights:1, people_count:1, idx_price:1}
      

      When I run a query with filter like this:

      db.cat_claim
      .explain("queryPlanner")
      .find({ 
      "checkin" : { "$in" : [ ISODate("2019-11-26T03:00:00.000+03:00"), ISODate("2019-11-27T03:00:00.000+03:00"), ISODate("2019-11-28T03:00:00.000+03:00"), ISODate("2019-11-29T03:00:00.000+03:00"), ISODate("2019-11-30T03:00:00.000+03:00"), ISODate("2019-12-01T03:00:00.000+03:00"), ISODate("2019-12-02T03:00:00.000+03:00"), ISODate("2019-12-03T03:00:00.000+03:00"), ISODate("2019-12-04T03:00:00.000+03:00"), ISODate("2019-12-05T03:00:00.000+03:00"), ISODate("2019-12-06T03:00:00.000+03:00"), ISODate("2019-12-07T03:00:00.000+03:00"), ISODate("2019-12-08T03:00:00.000+03:00"), ISODate("2019-12-09T03:00:00.000+03:00"), ISODate("2019-12-10T03:00:00.000+03:00"), ISODate("2019-12-11T03:00:00.000+03:00"), ISODate("2019-12-12T03:00:00.000+03:00"), ISODate("2019-12-13T03:00:00.000+03:00"), ISODate("2019-12-14T03:00:00.000+03:00"), ISODate("2019-12-15T03:00:00.000+03:00"), ISODate("2019-12-16T03:00:00.000+03:00"), ISODate("2019-12-17T03:00:00.000+03:00"), ISODate("2019-12-18T03:00:00.000+03:00"), ISODate("2019-12-19T03:00:00.000+03:00"), ISODate("2019-12-20T03:00:00.000+03:00"), ISODate("2019-12-21T03:00:00.000+03:00"), ISODate("2019-12-22T03:00:00.000+03:00"), ISODate("2019-12-23T03:00:00.000+03:00"), ISODate("2019-12-24T03:00:00.000+03:00"), ISODate("2019-12-25T03:00:00.000+03:00"), ISODate("2019-12-26T03:00:00.000+03:00") ] }, 
      "nights" : { "$in" : [ 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15 ] }, 
      "people_count" : 2 })
      .sort({"idx_price":1}) 
      .limit(600)

      Query planner shows SORT_KEY_GENERATOR and following SORT input stage instead of SORT_MERGE input stage. If I replace $in operators with array of $or conditions with the same conditions, query planner produces SORT_MERGE input stage.

        1. Example1.txt
          124 kB
        2. Example2.txt
          4 kB
        3. Example3.txt
          84 kB
        4. Example4.txt
          13 kB

            Assignee:
            jacob.evans@mongodb.com Jacob Evans
            Reporter:
            elohim-meth@yandex.ru Alexander Dubrovin
            Votes:
            0 Vote for this issue
            Watchers:
            8 Start watching this issue

              Created:
              Updated:
              Resolved: