dtp/mongo/client.go

28 lines
765 B
Go

package mongo
import (
"context"
"fmt"
"github.com/qiniu/qmgo"
"go.mongodb.org/mongo-driver/mongo/readpref"
goproto "google.golang.org/protobuf/proto"
)
func NewMongoClient() (*qmgo.Client, error) {
clientCfg := &qmgo.Config{
Uri: "mongodb://root:Wishpal2024@dds-bp1a72ede37c55d4242490.mongodb.rds.aliyuncs.com:3717/admin",
ConnectTimeoutMS: goproto.Int64(30000),
MaxPoolSize: goproto.Uint64(16),
MinPoolSize: goproto.Uint64(0),
SocketTimeoutMS: goproto.Int64(5000),
ReadPreference: &qmgo.ReadPref{Mode: readpref.PrimaryMode},
}
cli, err := qmgo.NewClient(context.Background(), clientCfg)
if err != nil {
fmt.Printf("NewMongoClient fail, cfg: %v, err: %v", clientCfg, err)
return nil, err
}
return cli, nil
}