From 88cb836e79fb86cac4db95b6ba7ad4bf35903622 Mon Sep 17 00:00:00 2001 From: Mohamed Khelif Date: Tue, 8 Oct 2024 10:16:48 -0400 Subject: [PATCH] DEVPROD-11817 Don't apply secondary _id sort if sort is by _id on hosts query (#8376) --- model/host/host.go | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/model/host/host.go b/model/host/host.go index 938f69f3937..e295c2316fd 100644 --- a/model/host/host.go +++ b/model/host/host.go @@ -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, })