dtp/mongo/client.go

28 lines
722 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://admin:Wishpal%402023@127.0.0.1:27017",
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
}