[GODRIVER-1904] Wrong sort sometimes when based on two fields Created: 02/Mar/21  Updated: 27/Oct/23  Resolved: 08/Mar/21

Status: Closed
Project: Go Driver
Component/s: API
Affects Version/s: 1.4.6
Fix Version/s: None

Type: Bug Priority: Major - P3
Reporter: Allen Gao Assignee: Unassigned
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-03-02-15-44-03-874.png     PNG File image-2021-03-02-15-44-45-097.png    

 Description   

I write a sample test on: https://github.com/GaoJianAllen/MongoTest

When use sort method to sort fields, it will return wrong sort result sometime.

Key code:

 

cursor, err := coll.Find(context.TODO(), bson.M{}, options.Find().SetSort(
   bson.M{"type": 1, "created": -1}))

Expect:

Actual(Sometimes):

 

Command is always correct.

db.coll.find().sort({"type": 1, "created_at":-1})

 



 Comments   
Comment by Allen Gao [ 05/Mar/21 ]

Hi Kevin, It resovled my problem. Thanks!

Comment by Kevin Albertson [ 04/Mar/21 ]

Hi jian.gao1@hp.com, thank you for the report! I believe this is due to using a bson.M as the sort document, which does not guarantee order of fields in the BSON document. You can alternatively use a bson.D to retain the key order.

cursor, err := coll.Find(context.TODO(), bson.M{}, options.Find().SetSort(bson.D{{"type", 1}, {"created", -1}})) 

The changes of GODRIVER-1810 warn if a bson.M is used as a sort parameter.

Generated at Thu Feb 08 08:37:23 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.