From d467381b6f8a89bcad99a3a11a6ee38b1880289c Mon Sep 17 00:00:00 2001 From: Monet Lee Date: Tue, 13 Aug 2024 17:09:11 +0800 Subject: [PATCH] update skip logic. --- pkg/common/storage/database/mgo/msg.go | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/pkg/common/storage/database/mgo/msg.go b/pkg/common/storage/database/mgo/msg.go index 3d3ab46e8e..c5aa635872 100644 --- a/pkg/common/storage/database/mgo/msg.go +++ b/pkg/common/storage/database/mgo/msg.go @@ -1231,6 +1231,7 @@ func (m *MsgMgo) GetDocIDs(ctx context.Context) ([]string, error) { limit := 5000 var skip int var docIDs []string + var offset int count, err := m.coll.CountDocuments(ctx, bson.M{}) if err != nil { @@ -1241,7 +1242,8 @@ func (m *MsgMgo) GetDocIDs(ctx context.Context) ([]string, error) { skip = 0 } else { rand.Seed(uint64(time.Now().UnixMilli())) - skip = rand.Intn(int(count)) + skip = rand.Intn(int(count / int64(limit))) + offset = skip * (int(count) / limit) } res, err := mongoutil.Aggregate[*model.MsgDocModel](ctx, m.coll, []bson.M{ @@ -1251,7 +1253,7 @@ func (m *MsgMgo) GetDocIDs(ctx context.Context) ([]string, error) { }, }, { - "$skip": skip, + "$skip": offset, }, { "$limit": limit,