扩展索引排序

This commit is contained in:
origin
2023-01-13 10:59:36 +08:00
parent 3833884777
commit b8150cfc51
2 changed files with 18 additions and 13 deletions

View File

@@ -68,34 +68,39 @@ func (s *Session) NextSeq(db string, collection string, id interface{}) (int, er
after := options.After after := options.After
updateOpts := options.FindOneAndUpdateOptions{ReturnDocument: &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 return res.Seq, err
} }
//indexKeys[索引][每个索引key字段] // indexKeys[索引][每个索引key字段]
func (s *Session) EnsureIndex(db string, collection string, indexKeys [][]string, bBackground bool,sparse bool) error { 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) return s.ensureIndex(db, collection, indexKeys, bBackground, false, sparse, asc)
} }
//indexKeys[索引][每个索引key字段] // indexKeys[索引][每个索引key字段]
func (s *Session) EnsureUniqueIndex(db string, collection string, indexKeys [][]string, bBackground bool,sparse bool) error { 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) return s.ensureIndex(db, collection, indexKeys, bBackground, true, sparse, asc)
} }
//keys[索引][每个索引key字段] // keys[索引][每个索引key字段]
func (s *Session) ensureIndex(db string, collection string, indexKeys [][]string, bBackground bool, unique bool,sparse bool) error { func (s *Session) ensureIndex(db string, collection string, indexKeys [][]string, bBackground bool, unique bool, sparse bool, asc bool) error {
var indexes []mongo.IndexModel var indexes []mongo.IndexModel
for _, keys := range indexKeys { for _, keys := range indexKeys {
keysDoc := bsonx.Doc{} keysDoc := bsonx.Doc{}
for _, key := range keys { 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 { if sparse == true {
options.SetSparse(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) ctxTimeout, cancel := context.WithTimeout(context.Background(), s.maxOperatorTimeOut)

View File

@@ -46,7 +46,7 @@ func (mp *MongoPersist) OnInit() error {
keys = append(keys, "Customer", "Topic") keys = append(keys, "Customer", "Topic")
IndexKey = append(IndexKey, keys) IndexKey = append(IndexKey, keys)
s := mp.mongo.TakeSession() 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()) log.SError("EnsureUniqueIndex is fail ", err.Error())
return err return err
} }