[GODRIVER-411] Working Find example with OptSort Created: 09/May/18  Updated: 27/Oct/23  Resolved: 31/May/18

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

Type: Task Priority: Minor - P4
Reporter: Thomas Geulen Assignee: Kristofer Brandow (Inactive)
Resolution: Works as Designed Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified


 Description   

I want use find with OptSort but this does not work as expected.

I want do the following in my golang application

db.getCollection('materialflow').find({station:1}).sort({outgoingtime:1})

When I create my options instancen with

sort, _ := mongo.Options{}.Sort(bson.NewDocument(bson.EC.Int32("station", 1)))
cursor, err := coll.Find(context.Background(), bson.NewDocument(bson.EC.String(key, id)), sort)

the find method returns 0 results.

Am I using this wrong?



 Comments   
Comment by Thomas Geulen [ 25/May/18 ]

Sorry, was my mistake. Works as expected

Comment by Kristofer Brandow (Inactive) [ 10/May/18 ]

Hi thomas.geulen,

I just threw together an example with this and it appears to be working for me. What version of Go, MongoDB, and the Go driver are you using?

Also, are you sure your document has a key that's a string and not an ObjectID?

--Kris

Comment by Thomas Geulen [ 09/May/18 ]

Sorry, there was a mistake in my previous example.

sort, _ := mongo.Options{}.Sort(bson.NewDocument(bson.EC.Int32("outgoingtime", 1)))   
cursor, err := coll.Find(context.Background(), bson.NewDocument(bson.EC.String(key, id)), sort)

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