This commit is contained in:
wangchuxiao
2022-05-06 15:58:50 +08:00
parent 243f5fcda4
commit 3c68ecb7f1
5 changed files with 139 additions and 62 deletions
+66 -12
View File
@@ -3,6 +3,7 @@ package db
import (
"Open_IM/pkg/common/config"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/x/bsonx"
//"Open_IM/pkg/common/log"
"Open_IM/pkg/utils"
@@ -73,12 +74,25 @@ func init() {
cSendLogModels := []mongo.IndexModel{
{
Keys: bson.M{"user_id": -1},
Keys: bsonx.Doc{
{
Key: "send_id",
},
},
Options: options.Index().SetUnique(true),
},
{
Keys: bsonx.Doc{
{
Key: "send_time",
Value: bsonx.Int32(-1),
},
},
},
}
result, err := dataBase.Collection(cSendLog).Indexes().CreateMany(context.Background(), cSendLogModels, opts)
if err != nil {
//fmt.Println("mongodb create cSendLogModels failed", result, err.Error())
fmt.Println("mongodb create cSendLogModels failed", result, err.Error())
}
cChatModels := []mongo.IndexModel{
@@ -93,28 +107,68 @@ func init() {
cWorkMomentModels := []mongo.IndexModel{
{
Keys: bson.M{"work_moment_id": -1},
Keys: bsonx.Doc{
{
Key: "create_time",
Value: bsonx.Int32(-1),
},
},
},
{
Keys: bson.M{"user_id": -1},
Keys: bsonx.Doc{
{
Key: "work_moment_id",
},
},
Options: options.Index().SetUnique(true),
},
}
cWorkMomentModel2 := []mongo.IndexModel{
{
Keys: bsonx.Doc{
{
Key: "work_moment_id",
},
},
Options: options.Index().SetUnique(true),
},
}
result, err = dataBase.Collection(cWorkMoment).Indexes().CreateMany(context.Background(), cWorkMomentModels, opts)
if err != nil {
//fmt.Println("mongodb create cWorkMomentModels failed", result, err.Error())
fmt.Println("mongodb create cWorkMomentModels failed", result, err.Error())
}
result, err = dataBase.Collection(cWorkMoment).Indexes().CreateMany(context.Background(), cWorkMomentModel2, opts)
if err != nil {
fmt.Println("mongodb create cWorkMomentModels failed", result, err.Error())
}
cTagModels := []mongo.IndexModel{
cTagModel1 := []mongo.IndexModel{
{
Keys: bson.M{"tag_id": -1},
},
{
Keys: bson.M{"user_id": -1},
Keys: bsonx.Doc{
{
Key: "tag_id",
},
},
Options: options.Index().SetUnique(true),
},
}
result, err = dataBase.Collection(cTag).Indexes().CreateMany(context.Background(), cTagModels, opts)
cTagModel2 := []mongo.IndexModel{
{
Keys: bsonx.Doc{
{
Key: "user_id",
},
},
Options: options.Index().SetUnique(true),
},
}
result, err = dataBase.Collection(cTag).Indexes().CreateMany(context.Background(), cTagModel1, opts)
if err != nil {
//fmt.Println("mongodb create cTagModels failed", result, err.Error())
fmt.Println("mongodb create cTagModel1 failed", result, err.Error())
}
result, err = dataBase.Collection(cTag).Indexes().CreateMany(context.Background(), cTagModel2, opts)
if err != nil {
fmt.Println("mongodb create cTagModel2 failed", result, err.Error())
}
DB.mongoClient = mongoClient
+14 -18
View File
@@ -666,25 +666,21 @@ func (d *DataBases) GetTagSendLogs(userID string, showNumber, pageNumber int32)
}
type WorkMoment struct {
WorkMomentID string `bson:"work_moment_id"`
UserID string `bson:"user_id"`
UserName string `bson:"user_name"`
FaceURL string `bson:"face_url"`
Content string `bson:"content"`
LikeUserList []*LikeUser `bson:"like_user_list"`
AtUserList []*AtUser `bson:"at_user_list"`
Comments []*Comment `bson:"comments"`
PermissionUserIDList []string `bson:"permission_user_id_list"`
Permission int32 `bson:"permission"`
CreateTime int32 `bson:"create_time"`
WorkMomentID string `bson:"work_moment_id"`
UserID string `bson:"user_id"`
UserName string `bson:"user_name"`
FaceURL string `bson:"face_url"`
Content string `bson:"content"`
LikeUserList []*WorkMomentUser `bson:"like_user_list"`
AtUserList []*WorkMomentUser `bson:"at_user_list"`
PermissionUserList []*WorkMomentUser `bson:"permission_user_list"`
Comments []*Comment `bson:"comments"`
PermissionUserIDList []string `bson:"permission_user_id_list"`
Permission int32 `bson:"permission"`
CreateTime int32 `bson:"create_time"`
}
type AtUser struct {
UserID string `bson:"user_id"`
UserName string `bson:"user_name"`
}
type LikeUser struct {
type WorkMomentUser struct {
UserID string `bson:"user_id"`
UserName string `bson:"user_name"`
}
@@ -749,7 +745,7 @@ func (d *DataBases) LikeOneWorkMoment(likeUserID, userName, workMomentID string)
}
}
if !isAlreadyLike {
workMoment.LikeUserList = append(workMoment.LikeUserList, &LikeUser{UserID: likeUserID, UserName: userName})
workMoment.LikeUserList = append(workMoment.LikeUserList, &WorkMomentUser{UserID: likeUserID, UserName: userName})
}
log.NewDebug("", utils.GetSelfFuncName(), workMoment)
ctx, _ := context.WithTimeout(context.Background(), time.Duration(config.Config.Mongo.DBTimeout)*time.Second)