Skip to content

Commit

Permalink
DEVPROD-11817 Don't apply secondary _id sort if sort is by _id on hos…
Browse files Browse the repository at this point in the history
…ts query (#8376)
  • Loading branch information
khelif96 authored Oct 8, 2024
1 parent beb80a2 commit 88cb836
Showing 1 changed file with 5 additions and 3 deletions.
8 changes: 5 additions & 3 deletions model/host/host.go
Original file line number Diff line number Diff line change
Expand Up @@ -3520,9 +3520,11 @@ func GetPaginatedRunningHosts(ctx context.Context, opts HostsFilterOptions) ([]H
if len(opts.SortBy) > 0 {
sorters = append(sorters, bson.E{Key: opts.SortBy, Value: opts.SortDir})
}
// _id must be the last item in the sort array to ensure a consistent sort
// order when previous sort keys result in a tie.
sorters = append(sorters, bson.E{Key: IdKey, Value: 1})

// If we're not sorting by ID, we need to sort by ID as a tiebreaker to ensure a consistent sort order.
if opts.SortBy != IdKey {
sorters = append(sorters, bson.E{Key: IdKey, Value: 1})
}
runningHostsPipeline = append(runningHostsPipeline, bson.M{
"$sort": sorters,
})
Expand Down

0 comments on commit 88cb836

Please sign in to comment.