This commit is contained in:
wangchuxiao 2022-05-25 17:46:02 +08:00
parent 72c7cc2c67
commit 76b5affaaa
2 changed files with 10 additions and 7 deletions

View File

@ -9,17 +9,14 @@ import (
"go.mongodb.org/mongo-driver/mongo/options" "go.mongodb.org/mongo-driver/mongo/options"
) )
var (
client *mongo.Client
)
func init() { func init() {
clientOptions := options.Client().ApplyURI("mongodb://127.0.0.1:37017/openIM/?maxPoolSize=100") clientOptions := options.Client().ApplyURI("mongodb://127.0.0.1:37017/openIM/?maxPoolSize=100")
client, err := mongo.Connect(context.TODO(), clientOptions) var err error
mongo2.Client, err = mongo.Connect(context.TODO(), clientOptions)
if err != nil { if err != nil {
panic(err) panic(err)
} }
err = client.Ping(context.TODO(), nil) err = mongo2.Client.Ping(context.TODO(), nil)
if err != nil { if err != nil {
panic(err) panic(err)
} }

View File

@ -4,15 +4,21 @@ import (
"Open_IM/pkg/common/config" "Open_IM/pkg/common/config"
"Open_IM/pkg/common/db" "Open_IM/pkg/common/db"
server_api_params "Open_IM/pkg/proto/sdk_ws" server_api_params "Open_IM/pkg/proto/sdk_ws"
"Open_IM/test/mongo/cmd"
"context" "context"
"fmt" "fmt"
"github.com/golang/protobuf/proto" "github.com/golang/protobuf/proto"
"go.mongodb.org/mongo-driver/bson/primitive" "go.mongodb.org/mongo-driver/bson/primitive"
"go.mongodb.org/mongo-driver/mongo"
"gopkg.in/mgo.v2/bson" "gopkg.in/mgo.v2/bson"
) )
var (
Client *mongo.Client
)
func GetUserAllChat(uid string) { func GetUserAllChat(uid string) {
collection := client.Database(config.Config.Mongo.DBDatabase).Collection("msg") collection := Client.Database(config.Config.Mongo.DBDatabase).Collection("msg")
var userChatList []db.UserChat var userChatList []db.UserChat
result, err := collection.Find(context.Background(), bson.M{"uid": primitive.Regex{Pattern: uid}}) result, err := collection.Find(context.Background(), bson.M{"uid": primitive.Regex{Pattern: uid}})
if err != nil { if err != nil {