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

View File

@ -69,7 +69,7 @@ func NewGroupServer(port int) *groupServer {
} }
mongo.InitMongo() mongo.InitMongo()
mongo.CreateSuperGroupIndex() mongo.CreateSuperGroupIndex()
g.GroupInterface = controller.NewGroupController(groupModel.DB, cache.InitRedis(), mongo.DB) g.GroupInterface = controller.NewGroupController(groupModel.DB, cache.InitRedis(), mongo.GetClient())
return &g return &g
} }

View File

@ -8,7 +8,6 @@ import (
"go.mongodb.org/mongo-driver/mongo" "go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options" "go.mongodb.org/mongo-driver/mongo/options"
"go.mongodb.org/mongo-driver/x/bsonx" "go.mongodb.org/mongo-driver/x/bsonx"
"log"
"strings" "strings"
"time" "time"
) )
@ -45,7 +44,7 @@ func (m *Mongo) InitMongo() {
config.Config.Mongo.DBMaxPoolSize) 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)) mongoClient, err := mongo.Connect(context.TODO(), options.Client().ApplyURI(uri))
if err != nil { if err != nil {
time.Sleep(time.Duration(30) * time.Second) time.Sleep(time.Duration(30) * time.Second)
@ -57,6 +56,10 @@ func (m *Mongo) InitMongo() {
m.DB = mongoClient m.DB = mongoClient
} }
func (m *Mongo) GetClient() *mongo.Client {
return m.DB
}
func (m *Mongo) CreateTagIndex() { func (m *Mongo) CreateTagIndex() {
if err := m.createMongoIndex(cSendLog, false, "send_id", "-send_time"); err != nil { if err := m.createMongoIndex(cSendLog, false, "send_id", "-send_time"); err != nil {
panic(err.Error() + " index create failed " + cSendLog + " send_id, -send_time") 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 { 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) opts := options.CreateIndexes().SetMaxTime(10 * time.Second)
indexView := db.Indexes() indexView := db.Indexes()
keysDoc := bsonx.Doc{} keysDoc := bsonx.Doc{}