# Insert if not exist or return the existent document

**URL:** <https://discuss.elastic.co/t/insert-if-not-exist-or-return-the-existent-document/144234>\
**Category:** Elasticsearch\
**Created:** [August 13, 2018, 8:15pm UTC](https://discuss.elastic.co/t/insert-if-not-exist-or-return-the-existent-document/144234 "2018-08-13T20:15:56Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Ranieri\_Mazili\_de\_Ol](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/ranieri_mazili_de_ol/32/15815_2.png) [@Ranieri\_Mazili\_de\_Ol](https://discuss.elastic.co/u/Ranieri_Mazili_de_Ol)\
**Post date:** [August 13, 2018, 8:15pm UTC](https://discuss.elastic.co/t/insert-if-not-exist-or-return-the-existent-document/144234/1 "2018-08-13T20:15:57Z")

</div>

unfortunatelly I haven't found the answer after some search and tries...

After insert the document below I received the id istiNGUBHVrkxpxfdqML

```
{
    "person" : {
        "brid" : "12345",
        "name" : "Jonh",
        "birthDate" : "1985-08-27",
        "expirationDate" : "2018-08-13" 
    }
}

```

I would like to know if is possible to deal with the following situation in only one request.

If the user tries to add a document with the same brid and current date is before expirationDate, return the content of document id istiNGUBHVrkxpxfdqML. If not, insert a new document and return the new document id.

Is this possible?

---

<div class="post-metadata">

**Author:** ![jaddison](https://avatars.discourse-cdn.com/v4/letter/j/e5b9ba/32.png) [@jaddison](https://discuss.elastic.co/u/jaddison)\
**Post date:** [September 9, 2018, 8:46pm UTC](https://discuss.elastic.co/t/insert-if-not-exist-or-return-the-existent-document/144234/2 "2018-09-09T20:46:34Z")

</div>

`op_type` is probably what you want to prevent an overwrite of the data; but you'd have to manually fetch the doc if it already exists. I would say that most systems expose a `get_or_create` type functionality would do it this way in behind the scenes.

[https://www.elastic.co/guide/en/elasticsearch/reference/current/docs-index\_.html#operation-type](https://www.elastic.co/guide/en/elasticsearch/reference/current/docs-index_.html#operation-type)

---

<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:** [October 7, 2018, 8:46pm UTC](https://discuss.elastic.co/t/insert-if-not-exist-or-return-the-existent-document/144234/3 "2018-10-07T20:46:44Z")

</div>

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