# Trying to rename all elements inside array

**URL:** <https://discuss.elastic.co/t/trying-to-rename-all-elements-inside-array/288884>\
**Category:** Logstash\
**Created:** [November 10, 2021, 11:42am UTC](https://discuss.elastic.co/t/trying-to-rename-all-elements-inside-array/288884 "2021-11-10T11:42:26Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Ankitkumar\_Ravidas](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/ankitkumar_ravidas/32/96953_2.png) [@Ankitkumar\_Ravidas](https://discuss.elastic.co/u/Ankitkumar_Ravidas)\
**Post date:** [November 10, 2021, 11:42am UTC](https://discuss.elastic.co/t/trying-to-rename-all-elements-inside-array/288884/1 "2021-11-10T11:42:26Z")

</div>

Hi team,

i have tags array in my mongodb contacts collection, which contain mongodb ids, its store as  
"tags":[{"$oid":"61822c908200c6b810bfee4d"},{"$oid":"61822c908200c6b810bfee4e"}]

 ![image](https://us1.discourse-cdn.com/elastic/original/3X/1/7/17f56bdfd3b37a3d1f97e8086a7427369ad1236f.png)

i want to rename all elements of mongo ids inside tags array to "documentId" like i did for \_id field :

filter {  
mutate {  
rename =\>{ "[\_id]" =\> "documentId"}  
}  
}

which give output as  
"documentId" =\> "6188d0be3d5bd73e9bb78dd6",

i am just failing in renaming all element of tags array which is also array of mongodb ids

please help

i am stuck since five days

here my logdash config file

input {  
mongodb{  
uri =\> 'mongodb://127.0.0.1:27017/makeEmails'  
placeholder\_db\_dir =\> '/opt/logstash/'  
placeholder\_db\_name =\> 'logstash\_sqlite.db'  
collection =\> 'contacts'  
batch\_size =\> 5000  
}  
}  
filter {  
mutate {  
rename =\>{ "[\_id]" =\> "documentId"}  
}  
}

output {  
stdout {  
codec =\> rubydebug  
}  
Elasticsearch {  
action =\> "index"  
index =\> "contact\_log"  
hosts =\> ["localhost:9200"]  
user =\> "elastic"  
password =\> "DYLDnjpyMHcnRbdfOckY"  
codec =\> json  
}  
}

---

<div class="post-metadata">

**Author:** ![Alex\_Marquardt](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/alex_marquardt/32/42925_2.png) [@Alex\_Marquardt](https://discuss.elastic.co/u/Alex_Marquardt)\
**Post date:** [November 10, 2021, 12:36pm UTC](https://discuss.elastic.co/t/trying-to-rename-all-elements-inside-array/288884/2 "2021-11-10T12:36:23Z")

</div>

> [@Ankitkumar\_Ravidas](#):
>
> 6188d0be3d5bd73e9bb78dd6

The following pipeline generates a sample document with an array called "tags", and then uses a ruby filter to replace the array elements with the "\_id" field. It then outputs the result to stdout.

```auto
input {
# The generator creates an input event
    generator {
        lines => [
            '{"tags": ["a", "b", "c"], "_id": "6188d0be3d5bd73e9bb78dd6"}'
        ]
        count => 1
        codec => "json"
    }
}

filter {
  if [tags] {
    ruby {
      code => '
        event.get("tags").each_with_index do |item, index|
          event.set("[tags][#{index}]", event.get("_id"))
        end
      '
    }
  }
}

output {
    stdout { codec => "rubydebug" }
}

```

The output from executing the above code is:

```auto
{
           "_id" => "6188d0be3d5bd73e9bb78dd6",
      "@version" => "1",
    "@timestamp" => 2021-11-10T12:33:37.454Z,
          "tags" => [
        [0] "6188d0be3d5bd73e9bb78dd6",
        [1] "6188d0be3d5bd73e9bb78dd6",
        [2] "6188d0be3d5bd73e9bb78dd6"
    ],
          "host" => "New2020MacBook",
      "sequence" => 0
}

```

---

<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:** [December 8, 2021, 12:37pm UTC](https://discuss.elastic.co/t/trying-to-rename-all-elements-inside-array/288884/3 "2021-12-08T12:37:08Z")

</div>

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