You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
Open-IM-Server/test/mongo/cmd/main.go

46 lines
1.4 KiB

2 years ago
package main
2 years ago
import (
2 years ago
"Open_IM/pkg/common/config"
2 years ago
mongo2 "Open_IM/test/mongo"
2 years ago
"context"
"flag"
"fmt"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options"
)
2 years ago
func init() {
2 years ago
uri := "mongodb://sample.host:27017/?maxPoolSize=20&w=majority"
if config.Config.Mongo.DBUri != "" {
// example: mongodb://$user:$password@mongo1.mongo:27017,mongo2.mongo:27017,mongo3.mongo:27017/$DBDatabase/?replicaSet=rs0&readPreference=secondary&authSource=admin&maxPoolSize=$DBMaxPoolSize
uri = config.Config.Mongo.DBUri
} else {
if config.Config.Mongo.DBPassword != "" && config.Config.Mongo.DBUserName != "" {
uri = fmt.Sprintf("mongodb://%s:%s@%s/%s?maxPoolSize=%d", config.Config.Mongo.DBUserName, config.Config.Mongo.DBPassword, config.Config.Mongo.DBAddress[0],
config.Config.Mongo.DBDatabase, config.Config.Mongo.DBMaxPoolSize)
} else {
uri = fmt.Sprintf("mongodb://%s/%s/?maxPoolSize=%d",
config.Config.Mongo.DBAddress[0], config.Config.Mongo.DBDatabase,
config.Config.Mongo.DBMaxPoolSize)
}
}
2 years ago
var err error
2 years ago
mongo2.Client, err = mongo.Connect(context.TODO(), options.Client().ApplyURI(uri))
2 years ago
if err != nil {
panic(err)
}
2 years ago
err = mongo2.Client.Ping(context.TODO(), nil)
2 years ago
if err != nil {
panic(err)
}
fmt.Println("Connected to MongoDB!")
}
func main() {
userID := flag.String("userID", "", "userID")
flag.Parse()
2 years ago
fmt.Println("userID:", *userID)
2 years ago
mongo2.GetUserAllChat(*userID)
2 years ago
}