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

Sort with limit by field with same values in aggregation

    XMLWordPrintableJSON

Details

    • Icon: Bug Bug
    • Resolution: Works as Designed
    • Icon: Major - P3 Major - P3
    • None
    • None
    • Querying
    • None
    • ALL

    Description

      I got this collection

      > db.foobar.find()
      { "_id" : ObjectId("585d0988e3f2e58d0f37946a"), "foo" : "bar", "index" : 1 }
      { "_id" : ObjectId("585d098be3f2e58d0f37946b"), "foo" : "bar", "index" : 2 }
      { "_id" : ObjectId("585d098de3f2e58d0f37946c"), "foo" : "bar", "index" : 3 }
      { "_id" : ObjectId("585d098fe3f2e58d0f37946d"), "foo" : "bar", "index" : 4 }
      { "_id" : ObjectId("585d0991e3f2e58d0f37946e"), "foo" : "bar", "index" : 5 }
      { "_id" : ObjectId("585d09bbe3f2e58d0f37946f"), "foo" : "baz", "index" : 6 }
      { "_id" : ObjectId("585d09c4e3f2e58d0f379470"), "foo" : "foo", "index" : 7 }
      

      When I try to sort in aggregation by foo fields it produces various results (just look at index field)

      > db.foobar.aggregate({$sort:{foo:1}},{$limit:2})
      { "_id" : ObjectId("585d0988e3f2e58d0f37946a"), "foo" : "bar", "index" : 1 }
      { "_id" : ObjectId("585d098be3f2e58d0f37946b"), "foo" : "bar", "index" : 2 }
      > db.foobar.aggregate({$sort:{foo:1}},{$limit:3})
      { "_id" : ObjectId("585d0988e3f2e58d0f37946a"), "foo" : "bar", "index" : 1 }
      { "_id" : ObjectId("585d098be3f2e58d0f37946b"), "foo" : "bar", "index" : 2 }
      { "_id" : ObjectId("585d098de3f2e58d0f37946c"), "foo" : "bar", "index" : 3 }
      > db.foobar.aggregate({$sort:{foo:1}},{$limit:4})
      { "_id" : ObjectId("585d098be3f2e58d0f37946b"), "foo" : "bar", "index" : 2 }
      { "_id" : ObjectId("585d098fe3f2e58d0f37946d"), "foo" : "bar", "index" : 4 }
      { "_id" : ObjectId("585d0988e3f2e58d0f37946a"), "foo" : "bar", "index" : 1 }
      { "_id" : ObjectId("585d098de3f2e58d0f37946c"), "foo" : "bar", "index" : 3 }
      > db.foobar.aggregate({$sort:{foo:1}},{$limit:5})
      { "_id" : ObjectId("585d098fe3f2e58d0f37946d"), "foo" : "bar", "index" : 4 }
      { "_id" : ObjectId("585d0991e3f2e58d0f37946e"), "foo" : "bar", "index" : 5 }
      { "_id" : ObjectId("585d098be3f2e58d0f37946b"), "foo" : "bar", "index" : 2 }
      { "_id" : ObjectId("585d0988e3f2e58d0f37946a"), "foo" : "bar", "index" : 1 }
      { "_id" : ObjectId("585d098de3f2e58d0f37946c"), "foo" : "bar", "index" : 3 }
      

      find+sort+limit works as expected

      > db.foobar.find().sort({foo:1}).limit(2)
      { "_id" : ObjectId("585d0988e3f2e58d0f37946a"), "foo" : "bar", "index" : 1 }
      { "_id" : ObjectId("585d098be3f2e58d0f37946b"), "foo" : "bar", "index" : 2 }
      > db.foobar.find().sort({foo:1}).limit(3)
      { "_id" : ObjectId("585d0988e3f2e58d0f37946a"), "foo" : "bar", "index" : 1 }
      { "_id" : ObjectId("585d098be3f2e58d0f37946b"), "foo" : "bar", "index" : 2 }
      { "_id" : ObjectId("585d098de3f2e58d0f37946c"), "foo" : "bar", "index" : 3 }
      > db.foobar.find().sort({foo:1}).limit(4)
      { "_id" : ObjectId("585d0988e3f2e58d0f37946a"), "foo" : "bar", "index" : 1 }
      { "_id" : ObjectId("585d098be3f2e58d0f37946b"), "foo" : "bar", "index" : 2 }
      { "_id" : ObjectId("585d098de3f2e58d0f37946c"), "foo" : "bar", "index" : 3 }
      { "_id" : ObjectId("585d098fe3f2e58d0f37946d"), "foo" : "bar", "index" : 4 }
      > db.foobar.find().sort({foo:1}).limit(5)
      { "_id" : ObjectId("585d0988e3f2e58d0f37946a"), "foo" : "bar", "index" : 1 }
      { "_id" : ObjectId("585d098be3f2e58d0f37946b"), "foo" : "bar", "index" : 2 }
      { "_id" : ObjectId("585d098de3f2e58d0f37946c"), "foo" : "bar", "index" : 3 }
      { "_id" : ObjectId("585d098fe3f2e58d0f37946d"), "foo" : "bar", "index" : 4 }
      { "_id" : ObjectId("585d0991e3f2e58d0f37946e"), "foo" : "bar", "index" : 5 }
      

      Attachments

        Activity

          People

            kelsey.schubert@mongodb.com Kelsey Schubert
            fleg Aleksey
            Votes:
            0 Vote for this issue
            Watchers:
            6 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved: