issue with $sort when used in combination with $skip and $limit

XMLWordPrintableJSON

    • Type: Bug
    • Resolution: Works as Designed
    • Priority: Major - P3
    • None
    • Affects Version/s: None
    • Component/s: None
    • None
    • Query Execution
    • ALL
    • Hide

      Run the following queries and see the results:
      Query 1:
      -----------------------------

      [
        {
          $match:
      {       "Data.general.assessmentId":         "08dd7293497b6bb7eabd888a",       "Data.general.status": "Certified"     }
        },
        {
          $sort:
      {       UpdatedOn: -1     }
        },
       
      {     $skip: 0   }
      ,
       
      {     $limit: 25   }
      ,
        {
           $match:
      {        "Data.general.userId":          "08dd08654ab95f1b27e45226"      }
        }
      ]
      

       

      Query 2:
      -----------------------------

      [
        {
          $match:
      {       "Data.general.assessmentId":         "08dd7293497b6bb7eabd888a",       "Data.general.status": "Certified"     }
        },
        {
          $sort:
      {       UpdatedOn: -1     }
        },
       
      {     $skip: 25   }
      ,
       
      {     $limit: 25   }
      ,
        {
           $match:
      {        "Data.general.userId":          "08dd08654ab95f1b27e45226"      }
        }
      ]
      

       

      Show
      Run the following queries and see the results: Query 1: ----------------------------- [   {     $match: {       "Data.general.assessmentId" :         "08dd7293497b6bb7eabd888a" ,       "Data.general.status" : "Certified"     }   },   {     $sort: {       UpdatedOn: -1     }   },   {     $skip: 0   } ,   {     $limit: 25   } ,   {      $match: {         "Data.general.userId" :           "08dd08654ab95f1b27e45226"      }   } ]   Query 2: ----------------------------- [   {     $match: {       "Data.general.assessmentId" :         "08dd7293497b6bb7eabd888a" ,       "Data.general.status" : "Certified"     }   },   {     $sort: {       UpdatedOn: -1     }   },   {     $skip: 25   } ,   {     $limit: 25   } ,   {      $match: {         "Data.general.userId" :           "08dd08654ab95f1b27e45226"      }   } ]  
    • None
    • 3
    • None
    • None
    • None
    • None
    • None
    • None
    • None

      When using the aggregation pipeline, the $skip stage does not correctly exclude documents based on the subsequent $match criteria. Even when specifying a valid skip value, documents with Data.general.userId set to 08dd08654ab95f1b27e45226 are still returned in the results. Sample document is attached, please investigate this issue.

        1. image-2025-04-24-10-20-40-517.png
          8 kB
          alex SL
        2. sahi-quiz.AssessmentStudent.json
          31.59 MB
          alex SL

            Assignee:
            Mihai Andrei
            Reporter:
            alex SL
            Votes:
            0 Vote for this issue
            Watchers:
            5 Start watching this issue

              Created:
              Updated:
              Resolved: