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.

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.

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. Is there a clean way to do this?

42 thoughts on “How to update a String with an Array in a document within Robo3T?”

  1. Basically you need to use $(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 }} )
    
    Reply

Leave a Comment