arraysdictionaryrethinkdbreql

How to Update nested Array in RethinkDB using ReQL


I have a question on Updating the array in RethinkDB. My JSON structure looks like below.

{
    "LOG_EVENT": {
        "ATTRIBUTES": [
            {
                "ATTRIBUTE1": "TYPE",
                "VALUE": "ORDER"
            },
            {
                "ATTRIBUTE2": "NUMBER",
                "VALUE": "1234567"
            }
        ],
        "EVENT_CODE": [
            {
                "CODE_NAME": "EVENT_SAVED",
                "EVENT_TIMESTAMP": "2015-08-18T00:58:12.421+08:00"                
            }

        ],
        "MSG_HEADER": {
            "BUSINESS_OBJ_TYPE": "order",
            "MSG_ID": "f79a672b-f15e-459d-a29b-725486d6401f",
            "DESTINATIONS": "3"            
        }
    },
    "id": "0de3117e-12dd-4d10-a464-dff391a4513f"
}

Here, I am trying to Update a new event inside my event code

{
   "CODE_NAME": "MESSAGE_DELIVERED_TO_APP2",
   "EVENT_TIMESTAMP": "2015-08-18T12:58:12.421+08:00"               
}

My final JSON will look like below,

{
    "LOG_EVENT": {
        "ATTRIBUTES": [
            {
                "ATTRIBUTE1": "TYPE",
                "VALUE": "ORDER"
            },
            {
                "ATTRIBUTE2": "NUMBER",
                "VALUE": "1234567"
            }
        ],
        "EVENT_CODE": [
            {
                "CODE_NAME": "EVENT_SAVED",
                "EVENT_TIMESTAMP": "2015-08-18T00:58:12.421+08:00"
            },
            {
                "CODE_NAME": "MESSAGE_DELIVERED_TO_APP2",
                "EVENT_TIMESTAMP": "2015-08-18T12:58:12.421+08:00"
            }
        ],
        "MSG_HEADER": {
            "BUSINESS_OBJ_TYPE": "order",
            "MSG_ID": "f79a672b-f15e-459d-a29b-725486d6401f",
            "DESTINATIONS": "3"
        }
    },
    "id": "0de3117e-12dd-4d10-a464-dff391a4513f"
}

Can you help on the ReQL query ?

Tried below, but not working

    r.db("test").table("test1").get("0de3117e-12dd-4d10-a464-dff391a4513f")("LOG_EVENT")('EVENT_CODE').update(function(row) {
  return {EVENT_CODE: row('EVENT_CODE').map(function(d) { 
    return r.branch(d.append({ 
     "CODE_NAME": "MESSAGE_DELIVERED_TO_APP2", 
     "EVENT_TIMESTAMP": "2015-08-18T00:58:12.421+08:00"      
    }), d)
      }) 
}} )

Solution

  • well here is the code which updates the nested fields of object residing inside an array

    r.db('DB').table('LOGS')
     .get('ID')
     .update({
        EVENT_CODE: r.row('EVENT_CODE')
                     .changeAt(1, r.row('EVENT_CODE').nth(1)
                                    .merge({"CODE_NAME": "MESSAGE_DELIVERED_TO_APP2"}))
            })