添加前缀到字段

huangapple go评论50阅读模式
英文:

Append Prefix to Field

问题

我有一些文档,例如:

{
    "_id" : "60d3fcb6077b0a41e090ecc9",
    "vendor" : "Best",
    "test_name" : "Number Sequence",
    "discipline" : "early math"
},
{
    "_id" : "60d3fcb6077b0a41e090edd7",
    "vendor" : "Best",
    "test_name" : "Value Places",
    "discipline" : "early math"
}

我想在test_name字段中附加EM:,无论该字段中已有什么内容,只要与vendordiscipline字段匹配。

到目前为止,我尝试了以下操作:

db.getCollection("assessments_1234").aggregate(
    [
        {
            "$match" : {
                "vendor" : "Best",
                "discipline" : "early math"
            }
        }, 
        {
            "$set" : {
                "test_name" : {
                    "$cond" : {
                        "if" : {
                            "$regexMatch" : {
                                "input" : "$test_name",
                                "regex" : /.* /
                            }
                        },
                        "then" : {
                            "$replaceOne" : {
                                "input" : "$test_name",
                                "find" : "",
                                "replacement" : "EM: "+"$test_name"
                            }
                        },
                        "else" : "$test_name"
                    }
                }
            }
        }
    ], 
    {
        "allowDiskUse" : false
    }
).forEach(function(record) {
    print(record.test_name)
    db.assessments_1234.replaceOne({_id: record._id}, record)
})
英文:

I have several documents such as:

    {
    "_id" : "60d3fcb6077b0a41e090ecc9",
    "vendor" : "Best",
    "test_name" : "Number Sequence",
    "discipline" : "early math"
},
{
    "_id" : "60d3fcb6077b0a41e090edd7",
    "vendor" : "Best",
    "test_name" : "Value Places",
    "discipline" : "early math"
}

I would like to append EM: to the test_name field, regardless of what is already in that field as long as it matches the vendor and discipline fields.

Here is what I have tried so far:

db.getCollection("assessments_1234").aggregate(
    [
        {
            "$match" : {
                "vendor" : "Best",
                "discipline" : "early math"
            }
        }, 
        {
            "$set" : {
                "test_name" : {
                    "$cond" : {
                        "if" : {
                            "$regexMatch" : {
                                "input" : "$test_name",
                                "regex" : /.* /
                            }
                        },
                        "then" : {
                            "$replaceOne" : {
                                "input" : "$test_name",
                                "find" : "",
                                "replacement" : "EM: "+"$test_name"
                            }
                        },
                        "else" : "$test_name"
                    }
                }
            }
        }
    ], 
    {
        "allowDiskUse" : false
    }
).forEach(function(record) {
    print(record.test_name)
   db.assessments_1234.replaceOne({_id: record._id}, record)
})

答案1

得分: 0

以下是您要翻译的部分:

  1. For the string concatenation, you should use $concat operator.
  2. As you want string concatenation, you should not need the $replaceOne operator.
db.getCollection("assessments_1234").update({
  "vendor": "Best",
  "discipline": "early math"
},
[
  {
    "$set": {
      "test_name": {
        "$cond": {
          "if": {
            "$regexMatch": {
              "input": "$test_name",
              "regex": ".*"
            }
          },
          "then": {
            $concat: [
              "EM: ",
              "$test_name"
            ]
          },
          "else": "$test_name"
        }
      }
    }
  }
],
{
  multi: true
})

Demo @ Mongo Playground

As you mentioned that you want to update the field_name without concern its value, perhaps you can simplify the query without the need of $cond operator.

db.getCollection("assessments_1234").update({
  "vendor": "Best",
  "discipline": "early math"
},
[
  {
    "$set": {
      "test_name": {
        $concat: [
          "EM: ",
          "$test_name"
        ]
      }
    }
  }
],
{
  multi: true
})

Demo (without $cond) @ Mongo Playground

英文:

Actually, you can work the update with the aggregation pipeline, which directly perform the update in the MongoDB server, without the need to fetch the record to the backend and update them one by one back to the MongoDB server.

  1. For the string concatenation, you should use $concat operator.
  2. As you want string concatenation, you should not need the $replaceOne operator.
db.getCollection("assessments_1234").update({
  "vendor": "Best",
  "discipline": "early math"
},
[
  {
    "$set": {
      "test_name": {
        "$cond": {
          "if": {
            "$regexMatch": {
              "input": "$test_name",
              "regex": ".*"
            }
          },
          "then": {
            $concat: [
              "EM: ",
              "$test_name"
            ]
          },
          "else": "$test_name"
        }
      }
    }
  }
],
{
  multi: true
})

Demo @ Mongo Playground

As you mentioned that you want to update the field_name without concern its value, perhaps you can simplify the query without the need of $cond operator.

db.getCollection("assessments_1234").update({
  "vendor": "Best",
  "discipline": "early math"
},
[
  {
    "$set": {
      "test_name": {
        $concat: [
          "EM: ",
          "$test_name"
        ]
      }
    }
  }
],
{
  multi: true
})

Demo (without $cond) @ Mongo Playground

huangapple
  • 本文由 发表于 2023年2月8日 08:51:20
  • 转载请务必保留本文链接:https://go.coder-hub.com/75380419.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定