-
Type: Bug
-
Resolution: Works as Designed
-
Priority: Major - P3
-
None
-
Affects Version/s: 3.1.1, 3.6.1
-
Component/s: JSON
-
None
Summary
I noticed this while looking into SERVER-34265, it looks like for some sortpatterns (possibly numeric fieldnames?) the correct ordering the of fields in the sortPattern may not be maintained.
pp.pprint(c.find({}).sort([ ("11", pymongo.DESCENDING), ("10", pymongo.ASCENDING)]).explain())
returns
u'winningPlan': {u'inputStage': {u'inputStage': {u'direction': u'forward', u'stage': u'COLLSCAN'}, u'stage': u'SORT_KEY_GENERATOR'}, u'sortPattern': {u'10': 1, u'11': -1}, u'stage': u'SORT'}},
The sort is correctly applied on the server-side, and the results of the query are appropriately ordered.
Additionally, I examined the Java driver's explain output, and it was fine, suggesting that the issue is in the python driver.
Standalone Repro
import pymongo import pprint pp = pprint.PrettyPrinter() client = pymongo.MongoClient('localhost', 27017) db = client.test c = db.foo c.insert({"10": 2, "11": 2}) c.insert({"10": 2, "11": 3}) c.insert({"10": 2, "11": 1}) c.insert({"10": 1, "11": 2}) c.insert({"10": 3, "11": 2}) for doc in c.find({}).sort([ ("11", pymongo.DESCENDING), ("10", pymongo.ASCENDING)]): print(doc) pp.pprint(c.find({}).sort([ ("11", pymongo.DESCENDING), ("10", pymongo.ASCENDING)]).explain())
- duplicates
-
PYTHON-596 Dictionaries do not preserve order
- Closed