# How to update field inside nested document?

**URL:** https://discuss.elastic.co/t/how-to-update-field-inside-nested-document/119202
**Category:** Elasticsearch
**Created:** [February 9, 2018, 9:44am UTC](https://discuss.elastic.co/t/how-to-update-field-inside-nested-document/119202 "2018-02-09T09:44:00Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![SreeAnnait](https://avatars.discourse-cdn.com/v4/letter/s/3ab097/32.png) [@SreeAnnait](https://discuss.elastic.co/u/SreeAnnait)
#### Post date: [February 9, 2018, 9:44am UTC](https://discuss.elastic.co/t/how-to-update-field-inside-nested-document/119202/1 "2018-02-09T09:44:00Z")

</div>

I have a document of below structure.

```
{
   	"OrderId": "2500001101518267176",
	"CustID":"6576788",
    "OrderDate": "2018-02-04T11:00:25.35",
    "ShippingMethod": "standard",
    "CertCode":"",
    "Status":"1",
    "LineItem": [
					{
						"lineNumber":1,
						"LineItemId": "169673001611457008",
						"ProductId": "189944947919967210",
						"PO":"ZAZ100012001",
						"CONumber":"",
						"MWWITemCode":""
                    },
                    {
                        "lineNumber":2,
						"LineItemId": "169673001611457002",
						"ProductId": "189944947919967210",
						"PO":"ZAZ100012001",
						"CONumber":"",
						"MWWITemCode":""
                    }
            	]
}

```

Here I am looking to update the 'CONumber' for the first document in the nested 'LineItem'.

Is there any option to update the this nested item via Update API?

---

<div class="post-metadata">

### Author: ![Magnus\_Kessler](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/magnus_kessler/32/42001_2.png) [@Magnus\_Kessler](https://discuss.elastic.co/u/Magnus_Kessler)
#### Post date: [February 9, 2018, 10:30am UTC](https://discuss.elastic.co/t/how-to-update-field-inside-nested-document/119202/2 "2018-02-09T10:30:05Z")

</div>

Have a look at scripted updates in the [Update API](https://www.elastic.co/guide/en/elasticsearch/reference/current/docs-update.html#_scripted_updates).

```auto
POST tmp_test/doc/1/_update
{
  "script": {
    "lang": "painless",
    "source": "ctx._source.LineItem[0].CONumber = params.CONumber",
    "params": {
      "CONumber": "9876"
    }
  }
}

```

---

<div class="post-metadata">

### Author: ![SreeAnnait](https://avatars.discourse-cdn.com/v4/letter/s/3ab097/32.png) [@SreeAnnait](https://discuss.elastic.co/u/SreeAnnait)
#### Post date: [February 9, 2018, 10:43am UTC](https://discuss.elastic.co/t/how-to-update-field-inside-nested-document/119202/3 "2018-02-09T10:43:55Z")

</div>

Awesome! Thank you Magnus\_Kessler.

I found a way to update with a where condition in the nested doc.

```
POST tmp_test/doc/1/_update
{
  
  "script": "for(i in ctx._source.LineItem){if(i.lineNumber==1){i.CONumber='123456'}}"

}

```

This is also working fine in my case.

---

<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: [March 9, 2018, 10:44am UTC](https://discuss.elastic.co/t/how-to-update-field-inside-nested-document/119202/4 "2018-03-09T10:44:17Z")

</div>

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