如何在 Robo3T 的文档中使用数组更新字符串?

How to update a String with an Array in a document within Robo3T?

我正在尝试更改 Robo3T 文档中数组内的元素对象。

结构如下所示:

  {
    "_id" : ObjectId("1234"),
    "source" : "BW",
    "sourceTableName" : "lwtrmls",
    "tableName" : "tier",
    "type" : "main",
    "primaryKeys" : [ 
        {
            "sourceField" : "tier_id", // <~~~~ This is what I am trying to update!
            "reportingField" : "bw_id",
            "type" : "integer"
        }
    ]
}

基本上试图将 primaryKeyssourceField 下的 tier_id 更改为 trmls_id

我试过 db.my_collection.update( {_id : "1234"}, {$set : {"primaryKeys.0.tier_id" : "trmls_id"}} ) 之类的方法,但似乎不起作用。有没有一种干净的方法可以做到这一点?

基本上你需要使用$(update)

在您的情况下,您需要执行当前查询:

db.my_collection.update( {_id : "1234", "primaryKeys.sourceField":"tier_id"}, {$set : {"primaryKeys.$.sourceField" : "trmls_id"}} )

已更新:

如果您不仅要更新当前过滤器数组中的第一个元素,而且要更新所有元素,您可以使用 $[]

db.my_collection.update( {_id : "1234", "primaryKeys.sourceField":"tier_id"}, {$set : {"primaryKeys.$[].sourceField" : "trmls_id"}, { multi: true }} )