Blog Detail

preview image DevOps
by Anurag Srivastava, Mar 9, 2019, 8:20:38 AM | 3 minutes

Configure Logstash to send MongoDB data into Elasticsearch

In this blog, I am going to explain how you can push your MongoDB data into Elasticsearch. You may be thinking what is the benefit of sending MongoDB data into Elasticsearch so let me explain to you the scenarios where you may want to push MongoDB data into Elasticsearch.

  • If you want to apply a robust search for your data.
  • You want to collect your data on a central Elasticsearch cluster for analysis and visualization.

Now to push the data into Elasticsearch you need "logstash-input-mongodb" input plugin for Logstash. So let us see how we can install this input plugin and can configure Logstash for pushing MongoDB data into Elasticsearch.


First, you have to log in as root user

sudo su


Then go to the Logstash installation directory (based on your operating system):

cd /usr/share/logstash


Now execute the following command:

bin/logstash-plugin install logstash-input-mongodb


You will get the following response:


Validating logstash-input-mongodb
Installing logstash-input-mongodb
Installation successful


Now create a configuration file to take MongoDB data as input.

input {
        uri => 'mongodb://usernam:password@anurag-00-00-no6gn.mongodb.net:27017/anurag?ssl=true'
        placeholder_db_dir => '/opt/logstash-mongodb/'
        placeholder_db_name => 'logstash_sqlite.db'
        collection => 'users'
        batch_size => 5000
}
filter {

}
output {
        stdout {
                codec => rubydebug
        }
        elasticsearch {
                action => "index"
                index => "mongo_log_data"
                hosts => ["localhost:9200"]
        }
}

After creating the configuration file execute the following command to pull the data.

bin/logstash -f /etc/logstash/conf.d/mongodata.conf

This command will start fetching from users collection as we have mentioned the name of the collection in the configuration file. This will also create an Elasticsearch index as "mongo_log_data" and will push the MongoDB data.

You will also get the output on the terminal as I have given two output blocks first for terminal output using stdout while other for Elasticsearch output.  This command will pull MongoDB data for user collection and will push the data into "mongo_log_data" index of Elasticsearch.

Other Blogs on Elastic Stack:
Introduction to Elasticsearch

Elasticsearch Installation and Configuration on Ubuntu 14.04
Log analysis with Elastic stack 
Elasticsearch Rest API
Basics of Data Search in Elasticsearch
Elasticsearch Rest API
Wildcard and Boolean Search in Elasticsearch
Configure Logstash to push MySQL data into Elasticsearch 
Metrics Aggregation in Elasticsearch
Bucket Aggregation in Elasticsearch
How to create Elasticsearch Cluster

If you found this article interesting, you can explore  "Mastering Kibana 6.0" and "Kibana 7 Quick Start Guide" to get more insight about Kibana and how we can configure ELK to create dashboards for key performance indicators.

About Author

Anurag Srivastava

Author | Blogger | Tech Lead | Elastic Stack | Innovator |

View Profile

Comments (2)

  • user image
    Johana Hernandez
    Jun 14, 2019, 3:21:09 PM

    Hello Anurag, I appreciate your input on this topic, I would like to know if you could help me with an error when executing the integration of MongoDB to ES .. I follow your recommendation in the configuration of the file but at the moment of reading from Mongo it remains in an infinite reading cycle. Annex the configuration file and the fragment of the cycle. input { mongodb{ uri => 'mongodb://user:password@server:27017/admin?connectTimeoutMS=10000&authSource=admin&authMechanism=SCRAM-SHA-1' placeholder_db_dir => '/Logstash/logstash-7.1.1/db_dir' placeholder_db_name => 'logstash_sqlite.db' collection => 'Monedas' batch_size => 3 generateId => true } } filter { mutate { remove_field => [ "_id" ] } } output { elasticsearch { action => "index" index => "indice_prueba" hosts => ["localhost:9200"] } } This fragment of error repeats itself incessantly. -------------------------------------------------------------------------------------------------------------------------------------------- [2019-06-14T10:32:13.006000 #35556] DEBUG -- : MONGODB | server:27017 | admin.listCollections | SUCCEEDED | 0.181s D, [2019-06-14T10:32:13.341000 #35556] DEBUG -- : MONGODB |server:27017 | admin.listCollections | STARTED | {"listCollections"=>1, "cursor"=>{}, "nameOnly"=>true, "filter"=>{"name"=>{"$not"=>/system\.|\$/}}} -------------------------------------------------------------------------------------------------------------------------------------------- Do you have any idea why this happens? Regards!

  • user image
    Anurag Srivastava
    Aug 14, 2019, 3:55:49 AM

    @johana: Please provide the Logstash configuration file content. Although it is too late but please let me know if this issue was resolved?

Leave a comment

Related Blogs

Snapshot and Restore Elasticsearch Indices

Sep 16, 2019, 5:55:06 AM | Anurag Srivastava

htop: An Interactive Process Viewer

Oct 13, 2018, 8:49:59 PM | Anurag Srivastava

Configure Logstash to push MySQL data into Elasticsearch

Feb 9, 2019, 12:06:18 PM | Anurag Srivastava

How to create Elasticsearch Cluster

Apr 6, 2019, 8:41:41 PM | Anurag Srivastava

Introduction to Elasticsearch

Apr 14, 2018, 1:18:05 PM | Anurag Srivastava

Log analysis with Elastic stack

Jan 31, 2018, 6:11:29 AM | Anurag Srivastava

Load csv Data into Elasticsearch

Feb 9, 2019, 6:34:22 PM | Anurag Srivastava

Configure Jenkins for Automated Code Deployment

Jun 13, 2018, 3:44:01 PM | Anurag Srivastava

Why SonarQube is important for IT projects ?

Apr 24, 2018, 2:52:28 PM | Anurag Srivastava

SonarQube installation on Ubuntu

May 12, 2018, 4:47:07 PM | Anurag Srivastava

Top Blogs

Configure SonarQube Scanner with Jenkins

Jun 21, 2018, 4:58:11 AM | Anurag Srivastava

Build and deploy Angular code using Python

Jun 26, 2018, 4:50:18 PM | Anurag Srivastava

Configure Jenkins for Automated Code Deployment

Jun 13, 2018, 3:44:01 PM | Anurag Srivastava

SonarQube installation on Ubuntu

May 12, 2018, 4:47:07 PM | Anurag Srivastava

Execute Commands on Remote Machines using sshpass

Jul 16, 2018, 5:00:02 PM | Anurag Srivastava

Why SonarQube is important for IT projects ?

Apr 24, 2018, 2:52:28 PM | Anurag Srivastava

Elasticsearch Rest API

Jul 31, 2018, 6:16:42 PM | Anurag Srivastava

Analyze your project with SonarQube

Jun 2, 2018, 10:49:54 AM | Anurag Srivastava

Install Jenkins on Ubuntu

May 26, 2018, 6:42:02 PM | Anurag Srivastava

Wildcard and Boolean Search in Elasticsearch

Aug 10, 2018, 7:14:40 PM | Anurag Srivastava