46 lines
1.4 KiB
Go
46 lines
1.4 KiB
Go
package main
|
|
|
|
import (
|
|
"Open_IM/pkg/common/config"
|
|
mongo2 "Open_IM/test/mongo"
|
|
"context"
|
|
"flag"
|
|
"fmt"
|
|
"go.mongodb.org/mongo-driver/mongo"
|
|
"go.mongodb.org/mongo-driver/mongo/options"
|
|
)
|
|
|
|
func init() {
|
|
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)
|
|
}
|
|
}
|
|
var err error
|
|
mongo2.Client, err = mongo.Connect(context.TODO(), options.Client().ApplyURI(uri))
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
err = mongo2.Client.Ping(context.TODO(), nil)
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
fmt.Println("Connected to MongoDB!")
|
|
}
|
|
|
|
func main() {
|
|
userID := flag.String("userID", "", "userID")
|
|
flag.Parse()
|
|
fmt.Println("userID:", *userID)
|
|
mongo2.GetUserAllChat(*userID)
|
|
}
|