From 296142f7c085ea20257d624a204c9dcf99923682 Mon Sep 17 00:00:00 2001 From: Sinh Date: Tue, 9 Nov 2021 11:04:49 +0700 Subject: [PATCH] add default sort latest --- action_query.go | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/action_query.go b/action_query.go index 65569c9..b17be4c 100644 --- a/action_query.go +++ b/action_query.go @@ -13,6 +13,7 @@ type AllQuery struct { TargetID string Page int64 Limit int64 + Sort interface{} } // All ... @@ -23,15 +24,16 @@ func (s Service) All(query AllQuery) []Audit { skip = query.Page * query.Limit result = make([]Audit, 0) ) + opts := options.Find().SetLimit(query.Limit).SetSkip(skip).SetSort(bson.M{"_id": -1}) + if query.Sort != nil { + opts.SetSort(query.Sort) + } // Find db cursor, err := s.DB.Collection(colName).Find(ctx, bson.D{ {"target", query.Target}, {"targetId", query.TargetID}, - }, &options.FindOptions{ - Limit: &query.Limit, - Skip: &skip, - }) + }, opts) if err != nil { return result }