# Update or remove element in an array of objects

**URL:** <https://discuss.elastic.co/t/update-or-remove-element-in-an-array-of-objects/136548>\
**Category:** Elasticsearch\
**Created:** [June 19, 2018, 6:01pm UTC](https://discuss.elastic.co/t/update-or-remove-element-in-an-array-of-objects/136548 "2018-06-19T18:01:37Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![jacktuck](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/jacktuck/32/32217_2.png) [@jacktuck](https://discuss.elastic.co/u/jacktuck)\
**Post date:** [June 19, 2018, 6:01pm UTC](https://discuss.elastic.co/t/update-or-remove-element-in-an-array-of-objects/136548/1 "2018-06-19T18:01:38Z")

</div>

My script fails with this error  
`{"type":"class_cast_exception","reason":"java.util.LinkedHashMap cannot be cast to java.lang.Number"}}}}}`

`,"errors":true,"items":[{"update":{"_index":"user","_type":"user","_id":"59706ad67313e0513619e333","status":400,"error":{"type":"illegal_argument_exception","reason":"failed to execute script","caused_by":{"type":"script_exception","reason":"runtime error","script_stack":["if (ctx._source._dms[i] && ctx._source._dms[i].user == params.dms.user) {\n "," ^---- HERE"],"script":"\n int found = 0;\n for(int i=0; i<ctx._source._dms.length; i++) {\n if (ctx._source._dms[i] && ctx._source._dms[i].user == params.dms.user) {\n ctx._source._dms[i] = params.dms;\n found = 1;\n }\n }\n \n ctx._source._dms.add(params.dms);\n ","lang":"painless","caused_by":{"type":"class_cast_exception","reason":"java.util.LinkedHashMap cannot be cast to java.lang.Number"}}}}},{"update":{"_index":"user","_type":"user","_id":"59706dab7313e0513619e334","status":400,"error":{"^C`

```auto
    const inline = `
      int found = 0;
      for(int i=0; i<ctx._source._dms.length; i++) {
        if (ctx._source._dms[i] && ctx._source._dms[i].user == params.dms.user) {
          ctx._source._dms[i] = params.dms;
          found = 1;
        }
      }
      
      ctx._source._dms.add(params.dms);
    `

    const body = [{
      update: {
        _index: 'user',
        _type: 'user',
        _id: firstUser.toString(),
        _retry_on_conflict: 10
      }
    }, {
      body: {
        script: {
          inline,
          params: {
            dms: {
              disabled: (disabledUsers || []).map(String).includes(secondUser.toString()),
              tag,
              user: secondUser.toString()
            }
          }
        }
      }
    }, {
      update: {
        _index: 'user',
        _type: 'user',
        _id: secondUser.toString(),
        _retry_on_conflict: 10
      }
    }, {
      body: {
        script: {
          inline,
          params: {
            dms: {
              disabled: (disabledUsers || []).map(String).includes(firstUser.toString()),
              tag,
              user: firstUser.toString()
            }
          }
        }
      }
    }]

```

Documents have an array of objects under the `_dms` field

```auto
    "_dms" : [
      {
        "disabled" : false,
        "tag" : "5979bc28a55761cfcb2a3a09",
        "user" : "597052c27313e0513619e332"
      }

```

```auto
 curl localhost:9200/user/user/_mapping?pretty
{
  "user" : {
    "mappings" : {
      "user" : {
        "properties" : {
          "_dms" : {
            "properties" : {
              "disabled" : {
                "type" : "boolean"
              },
              "tag" : {
                "type" : "text",
                "fields" : {
                  "keyword" : {
                    "type" : "keyword",
                    "ignore_above" : 256
                  }
                }
              },
              "user" : {
                "type" : "text",
                "fields" : {
                  "keyword" : {
                    "type" : "keyword",
                    "ignore_above" : 256
                  }
                }
              }
            }
          },
...

```

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [July 17, 2018, 6:01pm UTC](https://discuss.elastic.co/t/update-or-remove-element-in-an-array-of-objects/136548/2 "2018-07-17T18:01:42Z")

</div>

Closed on July 17, 2018, 6:01pm UTC

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.
