[SERVER-54359] $sort doesn't work in combination with $out Created: 05/Feb/21  Updated: 27/Oct/23  Resolved: 11/Feb/21

Status: Closed
Project: Core Server
Component/s: Aggregation Framework
Affects Version/s: 4.4.3
Fix Version/s: None

Type: Bug Priority: Major - P3
Reporter: Platon workaccount Assignee: Edwin Zhou
Resolution: Works as Designed Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Attachments: PNG File image-2021-02-05-21-49-55-431.png     PNG File src_unsrtd_coll.png     PNG File trg_srtd_table.png    
Operating System: ALL
Participants:

 Description   

Source collection with unsorted fields:

 

Results redirected to another database are not sorted. I suspect it's a bug.

[{'$match': {}}, {'$sort': SON([('chrom', 1), ('start', 1), ('end', 1)])}, {'$out': {'db': 'synth_BED_out', 'coll': 'bed_3_query_res.bed'}}]

 

Results, obtained through the cursor, are sorted. It's expected by me.

[{'$match': {}}, {'$sort': SON([('chrom', 1), ('start', 1), ('end', 1)])}]

 



 Comments   
Comment by Edwin Zhou [ 11/Feb/21 ]

Hi platon.work@gmail.com,

Thank you for the clarification. The order you're seeing in the database from the result of $out is $natural order, which is the default order returned by the query, which is what we see in your Compass screenshot. This ordering is an internal implementation feature, and you should not rely on any particular ordering of the documents.

We recommend explicitly sorting when querying the collection to provide your expected results.

Best,
Edwin

Comment by Platon workaccount [ 10/Feb/21 ]

SON is Python/BSON dict-like type.

Comment by Edwin Zhou [ 09/Feb/21 ]

Hi platon.work@gmail.com,

Thanks for reporting this behavior. I'm currently attempting to reproduce your issue but SON from the $sort stage of your pipeline appears to be invalid syntax.

Could you clarify that portion?

Best,
Edwin

Generated at Thu Feb 08 05:33:19 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.