This commit is contained in:
wangchuxiao
2023-01-29 11:30:12 +08:00
parent 52ceb7682d
commit b021089360
2 changed files with 7 additions and 4 deletions
+6 -3
View File
@@ -8,7 +8,6 @@ import (
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options"
"go.mongodb.org/mongo-driver/x/bsonx"
"log"
"strings"
"time"
)
@@ -45,7 +44,7 @@ func (m *Mongo) InitMongo() {
config.Config.Mongo.DBMaxPoolSize)
}
}
log.Println(utils.GetFuncName(1), "start to init mongoDB:", uri)
fmt.Println(utils.GetFuncName(1), "start to init mongoDB:", uri)
mongoClient, err := mongo.Connect(context.TODO(), options.Client().ApplyURI(uri))
if err != nil {
time.Sleep(time.Duration(30) * time.Second)
@@ -57,6 +56,10 @@ func (m *Mongo) InitMongo() {
m.DB = mongoClient
}
func (m *Mongo) GetClient() *mongo.Client {
return m.DB
}
func (m *Mongo) CreateTagIndex() {
if err := m.createMongoIndex(cSendLog, false, "send_id", "-send_time"); err != nil {
panic(err.Error() + " index create failed " + cSendLog + " send_id, -send_time")
@@ -97,7 +100,7 @@ func (m *Mongo) CreateWorkMomentIndex() {
}
func (m *Mongo) createMongoIndex(collection string, isUnique bool, keys ...string) error {
db := m.DB.Collection(collection)
db := m.DB.Database(config.Config.Mongo.DBDatabase).Collection(collection)
opts := options.CreateIndexes().SetMaxTime(10 * time.Second)
indexView := db.Indexes()
keysDoc := bsonx.Doc{}