Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

143
Views
MongoDB go driver maintain sort order

I query my records using this code:

cur, err := collection.Find(
    ctx,
    filter,
    options.Find().SetLimit(limit).SetSort(map[string]int{"timestamp": -1, "_id": -1}),
)

But I noticed through my mongodb log that the order changes sometimes... into _id: -1, timestamp: -1 which affects the query results. How do I make sure that timestamp comes first?

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

A map does not have any ordering guarantees for its elements. Use a bson.D for documents where element ordering is important:

SetSort({{"timestamp",-1},{"_id":-1}})
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!