arrayspushentitiesfiware-orion

Orion Context Broker - Push array element in entities


I made entity

{ "contextElements": [ { "type": "T1", "isPattern": "false", "id": "E1", "attributes": [ { "name": "A", "type": "T", "value": [ "22" , { "x": [ "x1", "x2"], "y": "3" }, [ "z1", "z2" ] ] }, { "name": "B", "type": "T", "value": { "x": { "x1": "a", "x2": "b" }, "y": [ "y1", "y2" ] } } ] } ], "updateAction": "APPEND" }

Now I want to add values in an array, a string ("NEW VALUE") and a json object ({"NEW":"OBJECT"})

{ "contextElements": [ { "type": "T1", "isPattern": "false", "id": "E1", "attributes": [ { "name": "A", "type": "T", "value": [ "22" , { "x": [ "x1", "x2"], "y": "3" }, [ "z1", "z2" ] , "NEW VALUE", {"NEW":"OBJECT"} ] } ] } ], "updateAction": "UPDATE" }

Is possible to add new values as you would do this in array.push()?

Save the previous values and add only new ones after.


Solution

  • At the present moment (Orion 0.18.1), the only way of adding adding an element to an attribute which value is a vector is to update such attribute with a new vector containing the new element.

    However, I think that pushing elements in attributes which value is a vector whitout needing to update the whole vector is an interesting feature to take into account, so we have created an issue about it and eventually it may be implemented.

    EDIT: this feature was finally implemented in Orion 3.3.0, using the $push update operator. For instance, to add item 3 to array attribute A in entity E:

    PUT /v2/entities/E/attrs/A
    {
      "value": { "$push": 3 },
      "type": "Number"
    }
    

    Full information can be found in Orion documentation.

    EDIT2: starting in Orion 3.5.0 you can add several items in the same operation using the $each modifier. For instance, to add items 3, 4 and 5 to array attribute A in entity E:

    POST /v2/entities/E/attrs/A
    {
      "value": { "$push": {"$each": [3, 4, 5]} },
      "type": "Number"
    }
    

    (Orion 3.5.0 is not yet released at the present moment, although the fix is already merged in master branch and can be got in telefonicaiot/fiware-orion:latest dockerhub image)