From 61070161263a01cab28dfa8f5dd668a2f51b82df Mon Sep 17 00:00:00 2001 From: qhd <1766646056@qq.com> Date: Fri, 27 Feb 2026 08:48:55 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96=20MongoDB=20=E5=91=BD?= =?UTF-8?q?=E4=BB=A4=E7=9B=91=E6=8E=A7=E6=97=A5=E5=BF=97=EF=BC=9A=E5=AE=89?= =?UTF-8?q?=E5=85=A8=E5=A4=84=E7=90=86=E9=9B=86=E5=90=88=E5=AD=97=E6=AE=B5?= =?UTF-8?q?=E5=B9=B6=E6=A0=87=E5=87=86=E5=8C=96=E6=97=A5=E5=BF=97=E8=BE=93?= =?UTF-8?q?=E5=87=BA?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- db/mongo/mongo.go | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/db/mongo/mongo.go b/db/mongo/mongo.go index 0ffb978..0c3d360 100644 --- a/db/mongo/mongo.go +++ b/db/mongo/mongo.go @@ -53,11 +53,18 @@ func commandMonitor() *event.CommandMonitor { return &event.CommandMonitor{ // 命令执行前触发 Started: func(ctx context.Context, evt *event.CommandStartedEvent) { - // 执行前的处理逻辑示例:记录开始时间、打印执行的命令 + // 1. 安全获取集合名:先判断字段是否存在,避免空值调用 + collectionName := "无" // 默认值 + collectionVal := evt.Command.Lookup("collection") + if !g.IsEmpty(collectionVal) { // 先检查是否为nil + collectionName = collectionVal.StringValue() + } + + // 2. 打印标准化日志(避免字段缺失导致的格式错乱) fmt.Printf("[%s] 开始执行命令 | 数据库: %s | 集合: %s | 命令: %+v\n", time.Now().Format("2006-01-02 15:04:05"), evt.DatabaseName, - //evt.Command.Lookup("collection").StringValue(), // 获取集合名 + collectionName, evt.Command, )