@Krishna_Chaitanya4 It’s not immediately clear what could be going wrong. The sort document looks correct
and the rest of the code you posted seems correct.
I have some questions to help troubleshoot:
- Can you give examples of the data in the collection you’re querying and examples of the out-of-order results you’re seeing?
- What version of the MongoDB Go Driver are you using?
- What version of MongoDB are you using?
- What database topology are you using? (e.g. standalone, replica set, sharded cluster, Atlas Serverless, etc)
I attempted to reproduce the issue you described using MongoDB 6.0 and Go Driver v1.12.1, but never got out-of-order results. Here’s the code I used to attempt to reproduce the issue:
func main() {
client, err := mongo.Connect(
context.Background(),
options.Client().ApplyURI("mongodb://localhost:27017/"))
if err != nil {
panic(err)
}
defer client.Disconnect(context.Background())
coll := client.Database("test").Collection("priority_order")
// Drop the collection so we start with an empty collection.
if err := coll.Drop(context.Background()); err != nil {
panic(err)
}
// Insert 20 docs with an integer "priority" field.
var docs []any
for i := 0; i < 20; i++ {
docs = append(docs, bson.D{{"priority", i}})
}
_, err = coll.InsertMany(context.Background(), docs)
if err != nil {
panic(err)
}
// Find the documents in reverse priority order, 2 at a time.
for i := 0; i < 10; i++ {
opts := options.Find().
SetSort(bson.D{{"priority", -1}, {"_id", 1}}).
SetLimit(2).
SetSkip(int64(i * 2))
cur, err := coll.Find(context.Background(), bson.D{}, opts)
if err != nil {
panic(err)
}
for cur.Next(context.Background()) {
fmt.Println(cur.Current)
}
if err := cur.Err(); err != nil {
panic(err)
}
}
}
// Output:
// {"_id": {"$oid":"64dfadfed96b783d87d5928a"},"priority": {"$numberInt":"19"}}
// {"_id": {"$oid":"64dfadfed96b783d87d59289"},"priority": {"$numberInt":"18"}}
// {"_id": {"$oid":"64dfadfed96b783d87d59288"},"priority": {"$numberInt":"17"}}
// {"_id": {"$oid":"64dfadfed96b783d87d59287"},"priority": {"$numberInt":"16"}}
// {"_id": {"$oid":"64dfadfed96b783d87d59286"},"priority": {"$numberInt":"15"}}
// {"_id": {"$oid":"64dfadfed96b783d87d59285"},"priority": {"$numberInt":"14"}}
// {"_id": {"$oid":"64dfadfed96b783d87d59284"},"priority": {"$numberInt":"13"}}
// {"_id": {"$oid":"64dfadfed96b783d87d59283"},"priority": {"$numberInt":"12"}}
// {"_id": {"$oid":"64dfadfed96b783d87d59282"},"priority": {"$numberInt":"11"}}
// {"_id": {"$oid":"64dfadfed96b783d87d59281"},"priority": {"$numberInt":"10"}}
// {"_id": {"$oid":"64dfadfed96b783d87d59280"},"priority": {"$numberInt":"9"}}
// {"_id": {"$oid":"64dfadfed96b783d87d5927f"},"priority": {"$numberInt":"8"}}
// {"_id": {"$oid":"64dfadfed96b783d87d5927e"},"priority": {"$numberInt":"7"}}
// {"_id": {"$oid":"64dfadfed96b783d87d5927d"},"priority": {"$numberInt":"6"}}
// {"_id": {"$oid":"64dfadfed96b783d87d5927c"},"priority": {"$numberInt":"5"}}
// {"_id": {"$oid":"64dfadfed96b783d87d5927b"},"priority": {"$numberInt":"4"}}
// {"_id": {"$oid":"64dfadfed96b783d87d5927a"},"priority": {"$numberInt":"3"}}
// {"_id": {"$oid":"64dfadfed96b783d87d59279"},"priority": {"$numberInt":"2"}}
// {"_id": {"$oid":"64dfadfed96b783d87d59278"},"priority": {"$numberInt":"1"}}
// {"_id": {"$oid":"64dfadfed96b783d87d59277"},"priority": {"$numberInt":"0"}}