# Transfering data from elasticsearch to mongodb

**URL:** <https://discuss.elastic.co/t/transfering-data-from-elasticsearch-to-mongodb/240689>\
**Category:** Logstash\
**Created:** [July 10, 2020, 11:31am UTC](https://discuss.elastic.co/t/transfering-data-from-elasticsearch-to-mongodb/240689 "2020-07-10T11:31:04Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![sandeepsingh](https://avatars.discourse-cdn.com/v4/letter/s/cab0a1/32.png) [@sandeepsingh](https://discuss.elastic.co/u/sandeepsingh)\
**Post date:** [July 10, 2020, 11:31am UTC](https://discuss.elastic.co/t/transfering-data-from-elasticsearch-to-mongodb/240689/1 "2020-07-10T11:31:04Z")

</div>

Hi,  
I am trying to extract data from elasticsearch to mongodb. I am using the inbuilt elasticsearch input plugin and 3.1.5 mongodb output plugin. I have multiple indexes in elasticsearch whose data needs to be extracted into different collections based on the index.  
What I have done is write a input config consisting of multiple inputs and a output consisting of multiple outputs, while using if and else conditions.

```auto
input {
  elasticsearch {
    hosts => "localhost:9243"
    user => "elastic"
    password => "abc"
    index => "index1"
    query => '{ "query": { "match_all": {} }}'
    size => 100
    docinfo => true
    ssl => true
    scroll => "1d"
  }
  elasticsearch {
    hosts => "localhost:9243"
    user => "elastic"
    password => "abc"
    index => "index2"
    query => '{ "query": { "match_all": {} }}'
    size => 100
    docinfo => true
    ssl => true
    scroll => "1d"
  }
  elasticsearch {
    hosts => "localhost:9243"
    user => "elastic"
    password => "abc"
    index => "index3"
    query => '{ "query": { "match_all": {} }}'
    size => 100
    docinfo => true
    ssl => true
    scroll => "1d"
  }
}

output {
  if [index] == "index1" {
    mongodb {
      id => "id1"
      bulk => true
      bulk_size => 100
      collection => "events"
      database => "database1"
      uri => "mongodb://user:password@localhost:27017/database1"
      codec => "json"
    }
  }
  else if [index] == "index2" {
    mongodb {
      id => "id2"
      bulk => true
      bulk_size => 100
      collection => "sessions"
      database => "database2"
      uri => "mongodb://user:password@localhost:27017/database2"
      codec => "json"
    }
  }
  else if [index] == "index3" {
    mongodb {
      id => "id3"
      bulk => true
      bulk_size => 100
      collection => "profile"
      database => "database3"
      uri => "mongodb://user:password@localhost:27017/database3"
      codec => "json"
    }
  }
}

```

However, this does not work and throws an error. But if I don't have if-else, only one output it works, but all three indexes data is extracted to a single collection. stdout on individual if-else, doesn't give any output and starts without error. It would be really great if someone could help.

---

<div class="post-metadata">

**Author:** ![Badger](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/badger/32/25190_2.png) [@Badger](https://discuss.elastic.co/u/Badger)\
**Post date:** [July 10, 2020, 4:55pm UTC](https://discuss.elastic.co/t/transfering-data-from-elasticsearch-to-mongodb/240689/2 "2020-07-10T16:55:17Z")

</div>

> [@sandeepsingh](#):
>
> this does not work and throws an error.

What error?

---

<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:** [August 7, 2020, 4:55pm UTC](https://discuss.elastic.co/t/transfering-data-from-elasticsearch-to-mongodb/240689/3 "2020-08-07T16:55:18Z")

</div>

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