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

$sort + $limit optimizer failing in some cases

    • Type: Icon: Bug Bug
    • Resolution: Duplicate
    • Priority: Icon: Major - P3 Major - P3
    • None
    • Affects Version/s: 3.4.6
    • Component/s: Aggregation Framework
    • None
    • Environment:
      Tested on Windows and Linux with MongoDB 3.4.6
    • ALL
    • Hide

      Import SortLimitBug dump in a new database.

      Run following aggregation and check Id of last record return (67)

      db.SortLimitBug.aggregate((
      [
                  { "$project" :
                    { "TM30" : 1, "OM30":1}
                  },
                  {
                      "$sort" : {
                          "TM30" : NumberInt(-1)
                      }
                  },
                  {
                      "$match" : {
                          "OM30" : {
                              "$lt" : 5.0
                          }
                      }
                  }, 
                  
                  {
                      "$skip" : NumberInt(0)
                  }, 
                  {
                      "$limit" : NumberInt(25)
                  }         
              ]))
      

      Run second page: Check Id of first record: is 67 instead of 72

      db.SortLimitBug.aggregate((
      [
                  { "$project" :
                    { "TM30" : 1, "OM30":1}
                  },
                  {
                      "$sort" : {
                          "TM30" : NumberInt(-1)
                      }
                  },
                  {
                      "$match" : {
                          "OM30" : {
                              "$lt" : 5.0
                          }
                      }
                  }, 
                  
                  {
                      "$skip" : NumberInt(25)
                  }, 
                  {
                      "$limit" : NumberInt(25)
                  }         
              ]))
       

      Run same queries without $limit and records will be correct

      Show
      Import SortLimitBug dump in a new database. Run following aggregation and check Id of last record return (67) db.SortLimitBug.aggregate(( [ { "$project" : { "TM30" : 1, "OM30" :1} }, { "$sort" : { "TM30" : NumberInt(-1) } }, { "$match" : { "OM30" : { "$lt" : 5.0 } } }, { "$skip" : NumberInt(0) }, { "$limit" : NumberInt(25) } ])) Run second page: Check Id of first record: is 67 instead of 72 db.SortLimitBug.aggregate(( [ { "$project" : { "TM30" : 1, "OM30" :1} }, { "$sort" : { "TM30" : NumberInt(-1) } }, { "$match" : { "OM30" : { "$lt" : 5.0 } } }, { "$skip" : NumberInt(25) }, { "$limit" : NumberInt(25) } ])) Run same queries without $limit and records will be correct

      We have discovered an issue that we think related to the $sort + $limit optimizer.
      We have attached dump of a sample collection.

      Second query with a limit of 25 has as first element the last element of the first page (25th) instead of the 26th correct one.

      Same queries performed without limit works properly.

        1. SortLimitBug.metadata.json.gz
          0.1 kB
        2. SortLimitBug.bson.gz
          1 kB

            Assignee:
            kelsey.schubert@mongodb.com Kelsey Schubert
            Reporter:
            ivan.fioravanti@4ward.it Ivan Fioravanti
            Votes:
            0 Vote for this issue
            Watchers:
            5 Start watching this issue

              Created:
              Updated:
              Resolved: