-
Type:
Bug
-
Resolution: Works as Designed
-
Priority:
Major - P3
-
None
-
Affects Version/s: None
-
Component/s: Querying
-
None
-
ALL
-
None
-
None
-
None
-
None
-
None
-
None
-
None
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 }