mirror of
https://gitee.com/dromara/mayfly-go
synced 2025-11-03 16:00:25 +08:00
feat: mongo优化
This commit is contained in:
@@ -16,8 +16,8 @@ type MongoCommand struct {
|
||||
}
|
||||
|
||||
type MongoRunCommand struct {
|
||||
Database string `binding:"required" json:"database"`
|
||||
Command map[string]any `json:"command"`
|
||||
Database string `binding:"required" json:"database"`
|
||||
Command []map[string]any `json:"command"`
|
||||
}
|
||||
|
||||
type MongoFindCommand struct {
|
||||
|
||||
@@ -91,12 +91,24 @@ func (m *Mongo) RunCommand(rc *req.Ctx) {
|
||||
commandForm := new(form.MongoRunCommand)
|
||||
ginx.BindJsonAndValid(rc.GinCtx, commandForm)
|
||||
cli := m.MongoApp.GetMongoCli(m.GetMongoId(rc.GinCtx))
|
||||
rc.ReqParam = commandForm
|
||||
|
||||
// 顺序执行
|
||||
commands := bson.D{}
|
||||
for _, cmd := range commandForm.Command {
|
||||
e := bson.E{}
|
||||
for k, v := range cmd {
|
||||
e.Key = k
|
||||
e.Value = v
|
||||
}
|
||||
commands = append(commands, e)
|
||||
}
|
||||
|
||||
ctx := context.TODO()
|
||||
var bm bson.M
|
||||
err := cli.Database(commandForm.Database).RunCommand(
|
||||
ctx,
|
||||
commandForm.Command,
|
||||
commands,
|
||||
).Decode(&bm)
|
||||
|
||||
biz.ErrIsNilAppendErr(err, "执行命令失败: %s")
|
||||
|
||||
@@ -17,6 +17,8 @@ func InitMongoRouter(router *gin.RouterGroup) {
|
||||
TagApp: tagapp.GetTagTreeApp(),
|
||||
}
|
||||
|
||||
saveDataPerm := req.NewPermission("mongo:data:save")
|
||||
|
||||
reqs := [...]*req.Conf{
|
||||
// 获取所有mongo列表
|
||||
req.NewGet("", ma.Mongos),
|
||||
@@ -39,13 +41,13 @@ func InitMongoRouter(router *gin.RouterGroup) {
|
||||
// 执行mongo find命令
|
||||
req.NewPost(":id/command/find", ma.FindCommand),
|
||||
|
||||
req.NewPost(":id/command/update-by-id", ma.UpdateByIdCommand).Log(req.NewLogSave("mongo-更新文档")),
|
||||
req.NewPost(":id/command/update-by-id", ma.UpdateByIdCommand).RequiredPermission(saveDataPerm).Log(req.NewLogSave("mongo-更新文档")),
|
||||
|
||||
// 执行mongo delete by id命令
|
||||
req.NewPost(":id/command/delete-by-id", ma.DeleteByIdCommand).Log(req.NewLogSave("mongo-删除文档")),
|
||||
req.NewPost(":id/command/delete-by-id", ma.DeleteByIdCommand).RequiredPermission(req.NewPermission("mongo:data:del")).Log(req.NewLogSave("mongo-删除文档")),
|
||||
|
||||
// 执行mongo insert 命令
|
||||
req.NewPost(":id/command/insert", ma.InsertOneCommand).Log(req.NewLogSave("mogno-插入文档")),
|
||||
req.NewPost(":id/command/insert", ma.InsertOneCommand).RequiredPermission(saveDataPerm).Log(req.NewLogSave("mogno-插入文档")),
|
||||
}
|
||||
|
||||
req.BatchSetGroup(m, reqs[:])
|
||||
|
||||
Reference in New Issue
Block a user