pull/455/head
wangchuxiao 3 years ago committed by Xinwei Xiong(cubxxw-openim)
parent 8511ea166b
commit d5ffbf4d67

@ -0,0 +1,33 @@
package mongo
import (
"context"
"flag"
"fmt"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options"
)
var (
client *mongo.Client
)
func initDB() {
clientOptions := options.Client().ApplyURI("mongodb://127.0.0.1:37017/openIM/?maxPoolSize=100")
client, err := mongo.Connect(context.TODO(), clientOptions)
if err != nil {
panic(err)
}
err = 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)
GetUserAllChat(*userID)
}

@ -0,0 +1,36 @@
package mongo
import (
"Open_IM/pkg/common/config"
"Open_IM/pkg/common/db"
server_api_params "Open_IM/pkg/proto/sdk_ws"
"context"
"fmt"
"github.com/golang/protobuf/proto"
"go.mongodb.org/mongo-driver/bson/primitive"
"gopkg.in/mgo.v2/bson"
)
func GetUserAllChat(uid string) {
collection := client.Database(config.Config.Mongo.DBDatabase).Collection("msg")
var userChatList []db.UserChat
result, err := collection.Find(context.Background(), bson.M{"uid": primitive.Regex{Pattern: uid}})
if err != nil {
fmt.Println(err.Error())
return
}
if err := result.All(context.Background(), &userChatList); err != nil {
fmt.Println(err.Error())
}
for _, userChat := range userChatList {
for _, msg := range userChat.Msg {
msgData := &server_api_params.MsgData{}
err := proto.Unmarshal(msg.Msg, msgData)
if err != nil {
fmt.Println(err.Error(), msg)
continue
}
fmt.Println(*msgData)
}
}
}
Loading…
Cancel
Save