Open leorochael opened 6 years ago
This issue has been reported in the forum, but has not been answered.
I'm not sure if it's applicable in a aggregate
query, but if you specify fields
value in your query, we use the order there for the columns order.
@arikfr,
I can confirm that when specifying a fields
dictionary/object in an aggregate
query, the order of the output columns match the sorting order of the values assigned to the column names in the fields
dictionary.
So this is a functional workaround, even though it requires mentioning the field names twice.
Would be nice if it was fixed though. If the order being lost is caused by a round-trip of the json of the query itself into a Python dict
, then this StackOverflow question discusses some solutions:
Upgrade to Python 3.6 or later, where the dicts are ordered by default.
Pass object_pairs_hook=collections.OrderedDict
as argument to json.loads()
or json.JSONDecoder()
(compatible all the way back to Python 2.7).
Issue Summary
When writing an
aggregate
query against a MongoDB datasource, the order of the columns cannot be chosen and is effectively random, even with a$project
step in theaggregate
pipeline specifying the order exactly.Changes in the
$project
keyword ordering causes stable but unpredictable changes in the ordering of the columns. It seems that the$project
keyword value is being round-tripped through a Python dictionary and losing it's original ordering.Steps to Reproduce
aggregate
query against a MongoDB collection$project
step renaming some columns$project
keyword.Technical details: