From b8150cfc510a32ef2bc34aed38b6db05ba4dabea Mon Sep 17 00:00:00 2001 From: origin Date: Fri, 13 Jan 2023 10:59:36 +0800 Subject: [PATCH] =?UTF-8?q?=E6=89=A9=E5=B1=95=E7=B4=A2=E5=BC=95=E6=8E=92?= =?UTF-8?q?=E5=BA=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- sysmodule/mongodbmodule/mongodbmodule.go | 29 +++++++++++-------- .../messagequeueservice/MongoPersist.go | 2 +- 2 files changed, 18 insertions(+), 13 deletions(-) diff --git a/sysmodule/mongodbmodule/mongodbmodule.go b/sysmodule/mongodbmodule/mongodbmodule.go index a882538..5088287 100644 --- a/sysmodule/mongodbmodule/mongodbmodule.go +++ b/sysmodule/mongodbmodule/mongodbmodule.go @@ -68,34 +68,39 @@ func (s *Session) NextSeq(db string, collection string, id interface{}) (int, er after := options.After updateOpts := options.FindOneAndUpdateOptions{ReturnDocument: &after} - err := s.Client.Database(db).Collection(collection).FindOneAndUpdate(ctxTimeout, bson.M{"_id": id}, bson.M{"$inc": bson.M{"Seq": 1}},&updateOpts).Decode(&res) + err := s.Client.Database(db).Collection(collection).FindOneAndUpdate(ctxTimeout, bson.M{"_id": id}, bson.M{"$inc": bson.M{"Seq": 1}}, &updateOpts).Decode(&res) return res.Seq, err } -//indexKeys[索引][每个索引key字段] -func (s *Session) EnsureIndex(db string, collection string, indexKeys [][]string, bBackground bool,sparse bool) error { - return s.ensureIndex(db, collection, indexKeys, bBackground, false,sparse) +// indexKeys[索引][每个索引key字段] +func (s *Session) EnsureIndex(db string, collection string, indexKeys [][]string, bBackground bool, sparse bool, asc bool) error { + return s.ensureIndex(db, collection, indexKeys, bBackground, false, sparse, asc) } -//indexKeys[索引][每个索引key字段] -func (s *Session) EnsureUniqueIndex(db string, collection string, indexKeys [][]string, bBackground bool,sparse bool) error { - return s.ensureIndex(db, collection, indexKeys, bBackground, true,sparse) +// indexKeys[索引][每个索引key字段] +func (s *Session) EnsureUniqueIndex(db string, collection string, indexKeys [][]string, bBackground bool, sparse bool, asc bool) error { + return s.ensureIndex(db, collection, indexKeys, bBackground, true, sparse, asc) } -//keys[索引][每个索引key字段] -func (s *Session) ensureIndex(db string, collection string, indexKeys [][]string, bBackground bool, unique bool,sparse bool) error { +// keys[索引][每个索引key字段] +func (s *Session) ensureIndex(db string, collection string, indexKeys [][]string, bBackground bool, unique bool, sparse bool, asc bool) error { var indexes []mongo.IndexModel for _, keys := range indexKeys { keysDoc := bsonx.Doc{} for _, key := range keys { - keysDoc = keysDoc.Append(key, bsonx.Int32(1)) + if asc { + keysDoc = keysDoc.Append(key, bsonx.Int32(1)) + } else { + keysDoc = keysDoc.Append(key, bsonx.Int32(-1)) + } + } - options:= options.Index().SetUnique(unique).SetBackground(bBackground) + options := options.Index().SetUnique(unique).SetBackground(bBackground) if sparse == true { options.SetSparse(true) } - indexes = append(indexes, mongo.IndexModel{Keys: keysDoc, Options:options }) + indexes = append(indexes, mongo.IndexModel{Keys: keysDoc, Options: options}) } ctxTimeout, cancel := context.WithTimeout(context.Background(), s.maxOperatorTimeOut) diff --git a/sysservice/messagequeueservice/MongoPersist.go b/sysservice/messagequeueservice/MongoPersist.go index c488f11..c329ccf 100644 --- a/sysservice/messagequeueservice/MongoPersist.go +++ b/sysservice/messagequeueservice/MongoPersist.go @@ -46,7 +46,7 @@ func (mp *MongoPersist) OnInit() error { keys = append(keys, "Customer", "Topic") IndexKey = append(IndexKey, keys) s := mp.mongo.TakeSession() - if err := s.EnsureUniqueIndex(mp.dbName, CustomerCollectName, IndexKey, true, true); err != nil { + if err := s.EnsureUniqueIndex(mp.dbName, CustomerCollectName, IndexKey, true, true,true); err != nil { log.SError("EnsureUniqueIndex is fail ", err.Error()) return err }