# Update a mongodb document using elasticsearch php

**URL:** <https://discuss.elastic.co/t/update-a-mongodb-document-using-elasticsearch-php/45129>\
**Category:** Elasticsearch\
**Created:** [March 22, 2016, 3:50pm UTC](https://discuss.elastic.co/t/update-a-mongodb-document-using-elasticsearch-php/45129 "2016-03-22T15:50:50Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![Satyadeep\_Kumar](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/satyadeep_kumar/32/8589_2.png) [@Satyadeep\_Kumar](https://discuss.elastic.co/u/Satyadeep_Kumar)\
**Post date:** [March 22, 2016, 3:50pm UTC](https://discuss.elastic.co/t/update-a-mongodb-document-using-elasticsearch-php/45129/1 "2016-03-22T15:50:50Z")

</div>

Hi,

I am trying to update a document based on \_id of the document using the following code:

```
require_once 'vendor/autoload.php';

    $client = Elasticsearch\ClientBuilder::create()->build();
    $params = [
        'index' => 'smartjn',
        'type' => 'user_master',
        'body' => [
            'query'=> [
                    'match'=> [
                                    'accessToken' => $accessToken
                    ]
            ]
        ]
       ];
    $results = $client->search($params);
    $result = $results['hits']['hits'][0]['_id'];

    $params = [
        'index' => 'smartjn',
        'type' => 'user_master',
        'body' => [
                             'update' => [
                                            '_id' => $result
                                          ]
                            ,
             'doc' => [
                'deviceId' => $deviceId
            ]
        ]
    ];
    $response = $client->update($params);
    var_dump($response);

```

The code is giving error on the **$client-\>update()** line . I am failing to understand the problem, any help will be greatly appreciated.

Thanks

---

<div class="post-metadata">

**Author:** ![Satyadeep\_Kumar](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/satyadeep_kumar/32/8589_2.png) [@Satyadeep\_Kumar](https://discuss.elastic.co/u/Satyadeep_Kumar)\
**Post date:** [March 23, 2016, 7:10am UTC](https://discuss.elastic.co/t/update-a-mongodb-document-using-elasticsearch-php/45129/2 "2016-03-23T07:10:35Z")

</div>

Any one , any help?

Thanks

---

<div class="post-metadata">

**Author:** ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)\
**Post date:** [March 23, 2016, 7:28am UTC](https://discuss.elastic.co/t/update-a-mongodb-document-using-elasticsearch-php/45129/3 "2016-03-23T07:28:59Z")

</div>

Please be patient when waiting for an answer to your questions. This is a community forum and as such it may take some time before someone replies to your question. Not everyone on the forum is an expert in every area so you may need to wait for someone who knows about the area you are asking about to come online and have the time to look into your problem.

Please see [https://www.elastic.co/community/codeofconduct](https://www.elastic.co/community/codeofconduct) for more details on our code of conduct (in particular the "be patient" section).

There are no SLAs on responses to questions posted on this forum, if you require help with an SLA on responses you should look into purchasing a subscription package that includes support with an SLA such as those offered by Elastic: [https://www.elastic.co/subscriptions](https://www.elastic.co/subscriptions)

By the way, you will get more chance of having someone answering if:

- you format your code. There is a button for that. `</>`
- you describe with more details what is happening. For example `The code is giving error` is very vague.

---

<div class="post-metadata">

**Author:** ![Satyadeep\_Kumar](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/satyadeep_kumar/32/8589_2.png) [@Satyadeep\_Kumar](https://discuss.elastic.co/u/Satyadeep_Kumar)\
**Post date:** [March 23, 2016, 7:32am UTC](https://discuss.elastic.co/t/update-a-mongodb-document-using-elasticsearch-php/45129/4 "2016-03-23T07:32:29Z")

</div>

mea culpa @dadoonet .

Please accept apologies for the same.

Thanks

---

<div class="post-metadata">

**Author:** ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)\
**Post date:** [March 23, 2016, 7:54am UTC](https://discuss.elastic.co/t/update-a-mongodb-document-using-elasticsearch-php/45129/5 "2016-03-23T07:54:48Z")

</div>

No problem. But can you reformat your code please?

---

<div class="post-metadata">

**Author:** ![Satyadeep\_Kumar](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/satyadeep_kumar/32/8589_2.png) [@Satyadeep\_Kumar](https://discuss.elastic.co/u/Satyadeep_Kumar)\
**Post date:** [March 23, 2016, 7:56am UTC](https://discuss.elastic.co/t/update-a-mongodb-document-using-elasticsearch-php/45129/6 "2016-03-23T07:56:27Z")

</div>

just did it. Thanks

---

<div class="post-metadata">

**Author:** ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)\
**Post date:** [March 23, 2016, 8:11am UTC](https://discuss.elastic.co/t/update-a-mongodb-document-using-elasticsearch-php/45129/7 "2016-03-23T08:11:37Z")

</div>

What is the error you are getting? Can you reproduce this without PHP but with a pure REST script so more people could help? (I'm not a PHP guy 🙂 )

---

<div class="post-metadata">

**Author:** ![Satyadeep\_Kumar](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/satyadeep_kumar/32/8589_2.png) [@Satyadeep\_Kumar](https://discuss.elastic.co/u/Satyadeep_Kumar)\
**Post date:** [March 26, 2016, 8:37am UTC](https://discuss.elastic.co/t/update-a-mongodb-document-using-elasticsearch-php/45129/8 "2016-03-26T08:37:49Z")

</div>

Anyone any help ? Was wondering whether I am going in right direction?

Thanks

---

<div class="post-metadata">

**Author:** ![Satyadeep\_Kumar](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/satyadeep_kumar/32/8589_2.png) [@Satyadeep\_Kumar](https://discuss.elastic.co/u/Satyadeep_Kumar)\
**Post date:** [March 29, 2016, 6:47am UTC](https://discuss.elastic.co/t/update-a-mongodb-document-using-elasticsearch-php/45129/9 "2016-03-29T06:47:06Z")

</div>

Nothing in yet ☹ . Any help is greatly appreciated.

Thanks

---

<div class="post-metadata">

**Author:** ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)\
**Post date:** [March 29, 2016, 7:33am UTC](https://discuss.elastic.co/t/update-a-mongodb-document-using-elasticsearch-php/45129/10 "2016-03-29T07:33:22Z")

</div>

I'm just going to copy and paste the same questions I asked.

> What is the error you are getting? Can you reproduce this without PHP but with a pure REST script so more people could help?

---

<div class="post-metadata">

**Author:** ![Satyadeep\_Kumar](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/satyadeep_kumar/32/8589_2.png) [@Satyadeep\_Kumar](https://discuss.elastic.co/u/Satyadeep_Kumar)\
**Post date:** [March 29, 2016, 4:27pm UTC](https://discuss.elastic.co/t/update-a-mongodb-document-using-elasticsearch-php/45129/11 "2016-03-29T16:27:26Z")

</div>

I do not see an error in **elasticsearch** as well as **mongodb** logs.

I can not reproduce the same bug using pure REST script as of now.

Thanks

---

<div class="post-metadata">

**Author:** ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)\
**Post date:** [March 29, 2016, 7:08pm UTC](https://discuss.elastic.co/t/update-a-mongodb-document-using-elasticsearch-php/45129/12 "2016-03-29T19:08:10Z")

</div>

So there is no error with elasticsearch.  
You don't see any error in elasticsearch or mongodb.

But you wrote that:

> The code is giving error on the $client-\>update() line.

So from where your error is coming from?

I'm sorry but we can't help without knowing what you are doing and what is exactly your problem.

---

<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 5, 2017, 11:04pm UTC](https://discuss.elastic.co/t/update-a-mongodb-document-using-elasticsearch-php/45129/13 "2017-07-05T23:04:17Z")

</div>


