简体   繁体   English

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

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

I am trying to change an element object that is within an array in a document on Robo3T.我正在尝试更改 Robo3T 文档中数组内的元素 object。

The structure looks like this:结构如下所示:

  {
    "_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"
        }
    ]
}

Basically trying to change tier_id under primaryKeys and sourceField to trmls_id .基本上试图将tier_idsourceField下的primaryKeys更改为trmls_id

I have tried something like db.my_collection.update( {_id: "1234"}, {$set: {"primaryKeys.0.tier_id": "trmls_id"}} ) and that does not seem to be working.我已经尝试过类似db.my_collection.update( {_id: "1234"}, {$set: {"primaryKeys.0.tier_id": "trmls_id"}} )的方法,但这似乎不起作用。 Is there a clean way to do this?有没有一种干净的方法可以做到这一点?

Basically you need to use $(update)基本上你需要使用$(update)

In you case you need to execute current query:在您的情况下,您需要执行当前查询:

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

Updated:更新:

If you want to update not only first element in array for current filters, but all, you could use $[]如果您不仅要更新当前过滤器的数组中的第一个元素,而且要更新所有元素,您可以使用$[]

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM