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

$skip followed by $limit in aggregation resort & lost records when $sort by equal values

    • Type: Icon: Bug Bug
    • Resolution: Works as Designed
    • Priority: Icon: Major - P3 Major - P3
    • None
    • Affects Version/s: 3.4.1
    • Component/s: Aggregation Framework
    • None
    • Environment:
      targetMinOS: Windows 7/Windows Server 2008 R2
      db version v3.4.1
      git version: 5e103c4f5583e2566a45d740225dc250baacfbd7
    • ALL
    • Hide
      > db.issue.find()
      
      { "_id" : ObjectId("58b3d25422ad9b04484145f9"), "author" :ObjectId("589d65ac10ee7a29a08e022a"), "type" : "customs", "date" : ISODate("2017-0227T07:16:36.173Z"), "likes" : [ ObjectId("589d65ac10ee7a29a08e022a") ], "visible" : 1,"country" : ObjectId("58a1a0bdc98ea02284fe077b"), "guideBody" : { "text" : "инструкция", "caption" : "Заголовок" }, "requireModeration" : 1, "rating" : 0 }
      { "_id" : ObjectId("58b3d25d22ad9b04484145fa"), "author" :ObjectId("589d65ac10ee7a29a08e022a"), "type" : "customs", "date" : ISODate("2017-0227T07:16:45.772Z"), "likes" : [ ], "visible" : 1, "country" :ObjectId("58a1a0bdc98ea02284fe077b"), "guideBody" : { "text" : "инструкция22", "caption": "Заголовок 22" }, "requireModeration" : 1, "rating" : 0 }
      { "_id" : ObjectId("58b3d2d122ad9b04484145fb"), "author" :ObjectId("589d65ac10ee7a29a08e022a"), "type" : "customs", "date" : ISODate("2017-0227T07:18:41.065Z"), "likes" : [ ObjectId("589d65ac10ee7a29a08e022a") ], "visible" : 1,"country" : ObjectId("58a1a0bdc98ea02284fe077b"), "guideBody" : { "text" : "что-то", "caption" : "Ииииха" }, "requireModeration" : 1, "rating" : 0 }
      { "_id" : ObjectId("58b9656e905c7a279806f8f6"), "author" :ObjectId("589d65ac10ee7a29a08e022a"), "type" : "customs", "date" : ISODate("2017-0303T12:45:34.161Z"), "likes" : [ ], "visible" : 1, "requireModeration" : 1, "country" :ObjectId("58a1a0bdc98ea02284fe077b"), "guideBody" : { "text" : "йцуйцу", "caption" :"йцуйцу" }, "rating" : 0 }
      { "_id" : ObjectId("58b96575905c7a279806f8f7"), "author" :ObjectId("589d65ac10ee7a29a08e022a"), "type" : "customs", "date" : ISODate("2017-0303T12:45:41.038Z"), "likes" : [ ], "visible" : 1, "requireModeration" : 1, "country" :ObjectId("58a1a0bdc98ea02284fe077b"), "guideBody" : { "text" : "йцуйцу11", "caption" :"йцуйцуй11" }, "rating" : 0 }
      { "_id" : ObjectId("58b96578905c7a279806f8f8"), "author" :ObjectId("589d65ac10ee7a29a08e022a"), "type" : "customs", "date" : ISODate("2017-0303T12:45:44.677Z"), "likes" : [ ], "visible" : 1, "requireModeration" : 1, "country" :ObjectId("58a1a0bdc98ea02284fe077b"), "guideBody" : { "text" : "йцуйцу1122", "caption" :"йцуйцуй1122" }, "rating" : 0 }
      { "_id" : ObjectId("58b96e44905c7a279806f8f9"), "author" :ObjectId("589d65ac10ee7a29a08e022a"), "type" : "customs", "date" : ISODate("2017-0303T13:23:16.176Z"), "likes" : [ ], "visible" : 1, "requireModeration" : 1, "country" :ObjectId("58a1a0bdc98ea02284fe077b"), "guideBody" : { "text" : "text", "caption" : "Title" }, "rating" : 10 }
      
      Show
      > db.issue.find() { "_id" : ObjectId("58b3d25422ad9b04484145f9"), "author" :ObjectId("589d65ac10ee7a29a08e022a"), "type" : "customs", "date" : ISODate("2017-0227T07:16:36.173Z"), "likes" : [ ObjectId("589d65ac10ee7a29a08e022a") ], "visible" : 1,"country" : ObjectId("58a1a0bdc98ea02284fe077b"), "guideBody" : { "text" : "инструкция", "caption" : "Заголовок" }, "requireModeration" : 1, "rating" : 0 } { "_id" : ObjectId("58b3d25d22ad9b04484145fa"), "author" :ObjectId("589d65ac10ee7a29a08e022a"), "type" : "customs", "date" : ISODate("2017-0227T07:16:45.772Z"), "likes" : [ ], "visible" : 1, "country" :ObjectId("58a1a0bdc98ea02284fe077b"), "guideBody" : { "text" : "инструкция22", "caption": "Заголовок 22" }, "requireModeration" : 1, "rating" : 0 } { "_id" : ObjectId("58b3d2d122ad9b04484145fb"), "author" :ObjectId("589d65ac10ee7a29a08e022a"), "type" : "customs", "date" : ISODate("2017-0227T07:18:41.065Z"), "likes" : [ ObjectId("589d65ac10ee7a29a08e022a") ], "visible" : 1,"country" : ObjectId("58a1a0bdc98ea02284fe077b"), "guideBody" : { "text" : "что-то", "caption" : "Ииииха" }, "requireModeration" : 1, "rating" : 0 } { "_id" : ObjectId("58b9656e905c7a279806f8f6"), "author" :ObjectId("589d65ac10ee7a29a08e022a"), "type" : "customs", "date" : ISODate("2017-0303T12:45:34.161Z"), "likes" : [ ], "visible" : 1, "requireModeration" : 1, "country" :ObjectId("58a1a0bdc98ea02284fe077b"), "guideBody" : { "text" : "йцуйцу", "caption" :"йцуйцу" }, "rating" : 0 } { "_id" : ObjectId("58b96575905c7a279806f8f7"), "author" :ObjectId("589d65ac10ee7a29a08e022a"), "type" : "customs", "date" : ISODate("2017-0303T12:45:41.038Z"), "likes" : [ ], "visible" : 1, "requireModeration" : 1, "country" :ObjectId("58a1a0bdc98ea02284fe077b"), "guideBody" : { "text" : "йцуйцу11", "caption" :"йцуйцуй11" }, "rating" : 0 } { "_id" : ObjectId("58b96578905c7a279806f8f8"), "author" :ObjectId("589d65ac10ee7a29a08e022a"), "type" : "customs", "date" : ISODate("2017-0303T12:45:44.677Z"), "likes" : [ ], "visible" : 1, "requireModeration" : 1, "country" :ObjectId("58a1a0bdc98ea02284fe077b"), "guideBody" : { "text" : "йцуйцу1122", "caption" :"йцуйцуй1122" }, "rating" : 0 } { "_id" : ObjectId("58b96e44905c7a279806f8f9"), "author" :ObjectId("589d65ac10ee7a29a08e022a"), "type" : "customs", "date" : ISODate("2017-0303T13:23:16.176Z"), "likes" : [ ], "visible" : 1, "requireModeration" : 1, "country" :ObjectId("58a1a0bdc98ea02284fe077b"), "guideBody" : { "text" : "text", "caption" : "Title" }, "rating" : 10 }

      Seems, issue exists only when you $sort records having equal value (rating here)

      >db.issue.aggregate( [ 
      { '$match':       { country: ObjectId("58a1a0bdc98ea02284fe077b"),        visible: 1,        type: 'customs' } },  
      { '$sort': { rating: -1 } },   
      { '$project': {_id:1}}
      ]);
      
      { "_id" : ObjectId("58b96e44905c7a279806f8f9") }
      { "_id" : ObjectId("58b3d25422ad9b04484145f9") }
      { "_id" : ObjectId("58b3d25d22ad9b04484145fa") }
      { "_id" : ObjectId("58b3d2d122ad9b04484145fb") }
      { "_id" : ObjectId("58b9656e905c7a279806f8f6") }
      { "_id" : ObjectId("58b96575905c7a279806f8f7") }
      { "_id" : ObjectId("58b96578905c7a279806f8f8") }
      
      > db.issue.aggregate( [ 
      { '$match':       { country: ObjectId("58a1a0bdc98ea02284fe077b"),        visible: 1,        type: 'customs' } },   
      { '$sort': { rating: -1 } },   
      { '$project': {_id:1}},   
      *{ '$skip': 0 },   
      { '$limit': 5 } *
      ] );
      
      { "_id" : ObjectId("58b96e44905c7a279806f8f9") }
      { "_id" : ObjectId("58b9656e905c7a279806f8f6") }
      { "_id" : ObjectId("58b96575905c7a279806f8f7") }
      { "_id" : ObjectId("58b3d25d22ad9b04484145fa") }
      { "_id" : ObjectId("58b3d25422ad9b04484145f9") }
      

      here we:

      • have resorted results
      • lost * { "_id" : ObjectId("58b3d2d122ad9b04484145fb") }

        *

      there is no this record on next 5

      >db.issue.aggregate( [
      { '$match':       { country: ObjectId("58a1a0bdc98ea02284fe077b"),        visible: 1,        type: 'customs' } },   
      { '$sort': { rating: -1 } },   
      { '$project': {_id:1}},  
      { '$skip': 5 },   
      { '$limit': 5 } 
      ] );
      
      { "_id" : ObjectId("58b96575905c7a279806f8f7") }
      { "_id" : ObjectId("58b96578905c7a279806f8f8") }
      

        1. test-28195.js
          4 kB
          Kelsey Schubert

            Assignee:
            kelsey.schubert@mongodb.com Kelsey Schubert
            Reporter:
            trueshura Alexandr
            Votes:
            0 Vote for this issue
            Watchers:
            9 Start watching this issue

              Created:
              Updated:
              Resolved: