英文:
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:,无论该字段中已有什么内容,只要与vendor和discipline字段匹配。
到目前为止,我尝试了以下操作:
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
以下是您要翻译的部分:
- For the string concatenation, you should use 
$concatoperator. - As you want string concatenation, you should not need the 
$replaceOneoperator. 
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
})
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.
- For the string concatenation, you should use 
$concatoperator. - As you want string concatenation, you should not need the 
$replaceOneoperator. 
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
})
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
})
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。


评论