我在 MongoDB 中遇到了 ReplaceAll 问题

问题描述 投票:0回答:1

我正在尝试附加文本

"[retrying: " + time.Now().Format("15:04") + "]"
,如果字段中缺少该文本,则会添加它;如果存在,它将被替换。

例如(添加文本):

{
  "_id": {
    "$oid": "66f134e7ad3dc5c40ced7b2a"
  },
  "CreatedAt": {
    "$date": "2024-09-23T09:29:11.276Z"
  },
  "UpdatedAt": {
    "$date": "2024-09-24T22:15:50.679Z"
  },
  "Step": "Waiting to receive information from the network"
}

目标:

{
  "_id": {
    "$oid": "66f134e7ad3dc5c40ced7b2a"
  },
  "CreatedAt": {
    "$date": "2024-09-23T09:29:11.276Z"
  },
  "UpdatedAt": {
    "$date": "2024-09-24T22:15:50.679Z"
  },
  "Step": "Waiting to receive information from the network [retrying 12:00]"
}

或者(替换文字)

{
  "_id": {
    "$oid": "66f134e7ad3dc5c40ced7b2a"
  },
  "CreatedAt": {
    "$date": "2024-09-23T09:29:11.276Z"
  },
  "UpdatedAt": {
    "$date": "2024-09-24T22:15:50.679Z"
  },
  "Step": "Waiting to receive information from the network [retrying 12:00]"
}

目标:

{
  "_id": {
    "$oid": "66f134e7ad3dc5c40ced7b2a"
  },
  "CreatedAt": {
    "$date": "2024-09-23T09:29:11.276Z"
  },
  "UpdatedAt": {
    "$date": "2024-09-24T22:15:50.679Z"
  },
  "Step": "Waiting to receive information from the network [retrying 12:05]"
}

目前我正在尝试使用以下代码:

fieldName := "$Step" 
retryingRegex := `\s\[retrying:.*\]`
retryingInfo := " [retrying: " + time.Now().Format("15:04") + "]"
update := bson.A{
    bson.M{
        "$set": bson.M{
            "Step": bson.M{
                "$cond": bson.M{
                    "if": bson.M{
                        "$regexMatch": bson.M{
                            "input": fieldName,
                            "regex": retryingRegex,
                        },
                    },
                    "then": bson.M{
                        "$replaceAll": bson.M{
                            "input":       fieldName,
                            "find":        retryingRegex,
                            "replacement": retryingInfo,
                        },
                    },
                    "else": bson.M{
                        "$concat": []string{
                            fieldName,
                            retryingInfo,
                        },
                    },
                },
            },
        },
    },
}
filter := bson.M{"_id": id}
collection.UpdateOne(ctx, filter, update)

但这似乎不起作用。 数据库版本v7.0.8 去版本1.22

mongodb go
1个回答
0
投票

问题在于,在

$replaceAll
操作中,
find
字段必须是要查找(和替换)的“纯”字符串。它不能是正则表达式。

您可以执行

$regexFind
来查找可更新
Step
字段的值中出现的部分,并将其替换。

但是

$regexpFind
不返回匹配的子字符串,它返回一个对象,其
match
字段包含匹配的子字符串。要获取其
match
字段,您可以使用
$getField

它可能是这样的:

"then": bson.M{
    "$replaceAll": bson.M{
        "input": fieldName,
        "find": bson.M{
            "$getField": bson.M{
                "field": "match",
                "input": bson.M{
                    "$regexFind": bson.M{
                        "input": fieldName,
                        "regex": retryingRegex,
                    },
                },
            },
        },
        "replacement": retryingInfo,
    },
},
© www.soinside.com 2019 - 2024. All rights reserved.