parent
1e43ad2fc0
commit
e103644929
@ -1,45 +0,0 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
mongoUtils "OpenIM/cmdutils/mongo"
|
||||
"OpenIM/pkg/common/config"
|
||||
"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,
|
||||
config.Config.Mongo.DBDatabase, config.Config.Mongo.DBMaxPoolSize)
|
||||
} else {
|
||||
uri = fmt.Sprintf("mongodb://%s/%s/?maxPoolSize=%d",
|
||||
config.Config.Mongo.DBAddress, config.Config.Mongo.DBDatabase,
|
||||
config.Config.Mongo.DBMaxPoolSize)
|
||||
}
|
||||
}
|
||||
var err error
|
||||
mongoUtils.Client, err = mongo.Connect(context.TODO(), options.Client().ApplyURI(uri))
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
err = mongoUtils.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)
|
||||
mongoUtils.GetUserAllChat(*userID)
|
||||
}
|
@ -1,59 +0,0 @@
|
||||
package mongo
|
||||
|
||||
import (
|
||||
"OpenIM/pkg/common/config"
|
||||
sdkws "OpenIM/pkg/proto/sdkws"
|
||||
"context"
|
||||
"fmt"
|
||||
"github.com/golang/protobuf/proto"
|
||||
"go.mongodb.org/mongo-driver/mongo"
|
||||
"gopkg.in/mgo.v2/bson"
|
||||
"time"
|
||||
)
|
||||
|
||||
var (
|
||||
Client *mongo.Client
|
||||
)
|
||||
|
||||
type MsgInfo struct {
|
||||
SendTime int64
|
||||
Msg []byte
|
||||
}
|
||||
|
||||
type UserChat struct {
|
||||
UID string
|
||||
Msg []MsgInfo
|
||||
}
|
||||
|
||||
func GetUserAllChat(uid string) {
|
||||
ctx, _ := context.WithTimeout(context.Background(), time.Duration(config.Config.Mongo.DBTimeout)*time.Second)
|
||||
collection := Client.Database(config.Config.Mongo.DBDatabase).Collection("msg")
|
||||
var userChatList []UserChat
|
||||
uid = uid + ":"
|
||||
filter := bson.M{"uid": bson.M{"$regex": uid}}
|
||||
//filter := bson.M{"uid": "17726378428:0"}
|
||||
result, err := collection.Find(context.Background(), filter)
|
||||
if err != nil {
|
||||
fmt.Println("find error", err.Error())
|
||||
return
|
||||
}
|
||||
if err := result.All(ctx, &userChatList); err != nil {
|
||||
fmt.Println(err.Error())
|
||||
}
|
||||
for _, userChat := range userChatList {
|
||||
for _, msg := range userChat.Msg {
|
||||
msgData := &sdkws.MsgData{}
|
||||
err := proto.Unmarshal(msg.Msg, msgData)
|
||||
if err != nil {
|
||||
fmt.Println(err.Error(), msg)
|
||||
continue
|
||||
}
|
||||
fmt.Println("seq: ", msgData.Seq, "status: ", msgData.Status,
|
||||
"sendID: ", msgData.SendID, "recvID: ", msgData.RecvID,
|
||||
"sendTime: ", msgData.SendTime,
|
||||
"clientMsgID: ", msgData.ClientMsgID,
|
||||
"serverMsgID: ", msgData.ServerMsgID,
|
||||
"content: ", string(msgData.Content))
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in new issue