# Sync from mongoDB to elasticsearch

**URL:** <https://discuss.elastic.co/t/sync-from-mongodb-to-elasticsearch/224023>\
**Category:** Elasticsearch\
**Created:** [March 18, 2020, 4:40am UTC](https://discuss.elastic.co/t/sync-from-mongodb-to-elasticsearch/224023 "2020-03-18T04:40:14Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![deamnguyen](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/deamnguyen/32/63941_2.png) [@deamnguyen](https://discuss.elastic.co/u/deamnguyen)\
**Post date:** [March 18, 2020, 4:40am UTC](https://discuss.elastic.co/t/sync-from-mongodb-to-elasticsearch/224023/1 "2020-03-18T04:40:14Z")

</div>

Hi all, I have questions about sync data from mongodb to elasticsearch, please give me permission:

1. There are many ways to sync, which one have i used?

- [Logstash](https://www.elastic.co/guide/en/logstash/7.1/introduction.html), [JDBC input plugin](https://www.elastic.co/guide/en/logstash/7.1/plugins-inputs-jdbc.html)

=\> Logstash file my config:

```auto
jdbc {
jdbc_driver_library => "mongojdbc1.8.jar"
    jdbc_driver_class => "com.dbschema.MongoJdbcDriver"
    jdbc_connection_string => "jdbc: mongodb://localhost:27017/tinh_huyen_xa "
    jdbc_user => ""
    jdbc_password => ""
    jdbc_paging_enabled => true
    schedule => "*/50 * * * * *"
    statement => 
    "
        db.ward.find({})
    "
  }

```

1. Error:

- I had a really stupid problem

`Only SELECT statements allowed with executeQuery()`  
\<I think, the logstash not allow run mongo code\>  
3. Question:

- The statement above I have omitted a lot (i need to handle a lot of nature). My question is how do i run the mongo code in logstash or do you have any suggestions?

Every answer is appreciated.

---

<div class="post-metadata">

**Author:** ![rameshkr1994](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/rameshkr1994/32/59029_2.png) [@rameshkr1994](https://discuss.elastic.co/u/rameshkr1994)\
**Post date:** [March 18, 2020, 9:04am UTC](https://discuss.elastic.co/t/sync-from-mongodb-to-elasticsearch/224023/2 "2020-03-18T09:04:10Z")

</div>

Hi @deamnguyen welcome 🙂 .

just take this connector from below link- and apply the same as you are doing-

> **[yougov/mongo-connector](https://github.com/yougov/mongo-connector)**
>
> MongoDB data stream pipeline tools by YouGov (adopted from MongoDB) - yougov/mongo-connector

i did the same with sql server-

```auto
input {
  jdbc {
    jdbc_driver_library => "C:\Users\Ramesh.kumar\Downloads\Elasticsearch\sqljdbc_4.2\enu\jre8\sqljdbc42.jar"
    #jdbc_driver_class => "com.microsoft.jdbc.sqlserver.SQLServerDriver"
	jdbc_driver_class => "com.microsoft.sqlserver.jdbc.SQLServerDriver"
    jdbc_connection_string => "zzzzzzzzzzzzzzzzzzzzzzzzzzz"
	#jdbc:sqlserver://databaseserver\instance;databaseName=test;integratedSecurity=true
    
    jdbc_user => "dser"
	jdbc_password => "dser"
	jdbc_validate_connection => true
	tracking_column => "dateinserted"
	 use_column_value =>true

    statement => "SELECT * FROM COURSE where dateinserted >:sql_last_value;"
	last_run_metadata_path => "C:\Users\Ramesh.kumar\.logstash_jdbc_last_run"
	#schedule => "* * * * * *"
  }
}

output {
  elasticsearch {
    document_id => "%{id}"
	document_type => "_doc"
    index => "dbdataindex_testauto"
	hosts => ["localhost:9200"]
	}
	stdout{
  codec => rubydebug
	
  }
}

```

Note-just replace lib. for mongo db and driver /user and pass.

Thanks  
HadoopHelp

---

<div class="post-metadata">

**Author:** ![deamnguyen](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/deamnguyen/32/63941_2.png) [@deamnguyen](https://discuss.elastic.co/u/deamnguyen)\
**Post date:** [March 22, 2020, 4:00pm UTC](https://discuss.elastic.co/t/sync-from-mongodb-to-elasticsearch/224023/3 "2020-03-22T16:00:22Z")

</div>

sorry for my delay, I'm grateful for your two option.

---

<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:** [April 19, 2020, 4:00pm UTC](https://discuss.elastic.co/t/sync-from-mongodb-to-elasticsearch/224023/4 "2020-04-19T16:00:46Z")

</div>

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