Skip to content

Commit 5af45c5

Browse files
committed
fix: duplicate sort key error in pagination function
1 parent cd5d2cb commit 5af45c5

File tree

1 file changed

+6
-1
lines changed

1 file changed

+6
-1
lines changed

mongox/pagination.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,13 @@ func (c *Collection) Paginate(ctx context.Context, rawFilter any, s *usecasex.So
4040
sortOrder *= -1
4141
}
4242

43+
sort := bson.D{{Key: sortKey, Value: sortOrder}}
44+
if sortKey != idKey {
45+
sort = append(sort, bson.E{Key: idKey, Value: sortOrder})
46+
}
47+
4348
findOpts := options.Find().
44-
SetSort(bson.D{{Key: sortKey, Value: sortOrder}, {Key: idKey, Value: sortOrder}}).
49+
SetSort(sort).
4550
SetLimit(limit(*p))
4651

4752
if p.Offset != nil {

0 commit comments

Comments
 (0)