# Adding a new field to two billion documents

**URL:** <https://discuss.elastic.co/t/adding-a-new-field-to-two-billion-documents/132162>\
**Category:** Elasticsearch\
**Created:** [May 16, 2018, 3:15pm UTC](https://discuss.elastic.co/t/adding-a-new-field-to-two-billion-documents/132162 "2018-05-16T15:15:29Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![kurtiskurtis](https://avatars.discourse-cdn.com/v4/letter/k/45deac/32.png) [@kurtiskurtis](https://discuss.elastic.co/u/kurtiskurtis)\
**Post date:** [May 16, 2018, 3:15pm UTC](https://discuss.elastic.co/t/adding-a-new-field-to-two-billion-documents/132162/1 "2018-05-16T15:15:29Z")

</div>

After migrating nearly 2billion documents I noticed I DERPED and forgot to set a field we call "mongo\_id" on 90% of them.

Is there a way to update all of the documents so that

document.mongo\_id = document.meta.id

---

<div class="post-metadata">

**Author:** ![Christian\_Dahlqvist](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/christian_dahlqvist/32/4617_2.png) [@Christian\_Dahlqvist](https://discuss.elastic.co/u/Christian_Dahlqvist)\
**Post date:** [May 16, 2018, 3:30pm UTC](https://discuss.elastic.co/t/adding-a-new-field-to-two-billion-documents/132162/2 "2018-05-16T15:30:36Z")

</div>

Any update will require reindexing.

---

<div class="post-metadata">

**Author:** ![kurtiskurtis](https://avatars.discourse-cdn.com/v4/letter/k/45deac/32.png) [@kurtiskurtis](https://discuss.elastic.co/u/kurtiskurtis)\
**Post date:** [May 16, 2018, 3:35pm UTC](https://discuss.elastic.co/t/adding-a-new-field-to-two-billion-documents/132162/3 "2018-05-16T15:35:26Z")

</div>

That is fine I suppose, do you happen to have an example query on how a field can be set to the value of a documents id?

---

<div class="post-metadata">

**Author:** ![kurtiskurtis](https://avatars.discourse-cdn.com/v4/letter/k/45deac/32.png) [@kurtiskurtis](https://discuss.elastic.co/u/kurtiskurtis)\
**Post date:** [May 16, 2018, 4:09pm UTC](https://discuss.elastic.co/t/adding-a-new-field-to-two-billion-documents/132162/4 "2018-05-16T16:09:12Z")

</div>

```
POST indexname/doctype/_update_by_query
{
  "script": {
    "source": "ctx._source.id = ctx._id",
    "lang": "painless"
  },
  "query":{
    "bool":{
        "must_not":{
          "exists":{
          "field":"mongo_id"
          }
        }
    }
  }
}
```

---

<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:** [June 13, 2018, 4:18pm UTC](https://discuss.elastic.co/t/adding-a-new-field-to-two-billion-documents/132162/5 "2018-06-13T16:18:33Z")

</div>

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