[SERVER-27511] Sort with limit by field with same values in aggregation Created: 23/Dec/16  Updated: 27/Oct/23  Resolved: 04/Jan/17

Status: Closed
Project: Core Server
Component/s: Querying
Affects Version/s: None
Fix Version/s: None

Type: Bug Priority: Major - P3
Reporter: Aleksey Assignee: Kelsey Schubert
Resolution: Works as Designed Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Operating System: ALL
Participants:

 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 }



 Comments   
Comment by Kelsey Schubert [ 04/Jan/17 ]

Hi fleg,

This is the expected behavior as both results are sorted by foo as requested. It's true that the results are not always sorted by the index field, but there is no expectation that it would since the command did not ask for such a sort.

For MongoDB-related support discussion please post on the mongodb-user group or Stack Overflow with the mongodb tag. A question like this involving more discussion would be best posted on the mongodb-users group.

Kind regards,
Thomas

Comment by Aleksey [ 23/Dec/16 ]

$ mongo --version
MongoDB shell version: 3.2.5
$ mongod --version
db version v3.2.5
git version: 34e65e5383f7ea1726332cb175b73077ec4a1b02
OpenSSL version: OpenSSL 1.0.1f 6 Jan 2014
allocator: tcmalloc
modules: none
build environment:
    distmod: ubuntu1404
    distarch: x86_64
    target_arch: x86_64

Generated at Thu Feb 08 04:15:21 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.