# What is the best way to push/add new elements into a nested array of objects?

**URL:** <https://discuss.elastic.co/t/what-is-the-best-way-to-push-add-new-elements-into-a-nested-array-of-objects/263439>\
**Category:** Elasticsearch\
**Created:** [February 5, 2021, 4:47pm UTC](https://discuss.elastic.co/t/what-is-the-best-way-to-push-add-new-elements-into-a-nested-array-of-objects/263439 "2021-02-05T16:47:06Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![OB1290](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/ob1290/32/83316_2.png) [@OB1290](https://discuss.elastic.co/u/OB1290)\
**Post date:** [February 5, 2021, 4:47pm UTC](https://discuss.elastic.co/t/what-is-the-best-way-to-push-add-new-elements-into-a-nested-array-of-objects/263439/1 "2021-02-05T16:47:07Z")

</div>

I have a candidate Index where documents are structured like so :

```auto
{
  "_index" : "candidates",
  "_type" : "_doc",
  "_id" : "a23OcncBXBMGOH6pwXge",
  "_version" : 1,
  "_seq_no" : 1,
  "_primary_term" : 1,
  "found" : true,
  "_source" : {
    "id" : "413",
    "firstname" : "Tania",
    "lastname" : "Stroman",
    "email" : "Berlin@yahoo.com",
    "zip" : "60306",
    "city" : "frankfurt",
    "birthday" : "1978-11-22",
    "tags" : [
      "php",
      "munich"
    ],
    "location" : {
      "lat" : 50.11601257324219,
      "lon" : 8.669955253601074
    }
  }
}

```

Once a candidate uploads their file, I add a field called 'file' to the elastic document for the first one and push the subsequent uploads to the array with the following code :

```auto
public function attachCandidateFile($file)
    {
        $client = ClientBuilder::create()->build();

        $params = [
            'index' => 'candidates',
            'id' => $file['candidate_id']
        ];

        $candidate = $client->get($params);

        if (!is_array($candidate['_source']['file'])) {
            $candidate['_source']['file'] = [];
        } 
        array_push($candidate['_source']['file'], $file);

        $params = [
            'index' => 'candidates',
            'type' => '_doc',
            'id' => $file['candidate_id'],   
            'body' => [
                'doc' => [
                    'file' => $candidate['_source']['file']
                ]
            ]                
           
        ];
        $response = $client->update($params);
        echo '<pre>', print_r($response, true) ,'</pre>';
    } 

```

Is there a way to update the candidate directly with the new object without needing the fetch `$candidate['_source']['file']` so as to avoid overriding it ?

---

<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 5, 2021, 4:47pm UTC](https://discuss.elastic.co/t/what-is-the-best-way-to-push-add-new-elements-into-a-nested-array-of-objects/263439/2 "2021-03-05T16:47:57Z")

</div>

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