Photo by Mihai Varga on Unsplash

In another article we saw how to create a twitter bot that sends private messages to his followers, containing tweets searched by predefined criteria — certain hashtags selected by us. This time we will improve the reports being sent, by adding additional functionality. We will store the results in a database and filter the newly received tweets according to what we have stored. Thus, we will be able to send only (almost)* unique results.


This is the second part of a step by step tutorial showing you how to create your own Twitter bot and host it on heroku. This time we will upgrade our code by using mongoose and MongoDB to produce better results. Also, we will extend the usage of our helpers, by extracting there the composing of the messages content. In the end, we will see how can we use Mlab hosting service (free plan) to host our database there.

If you want to skip right to the code, here is the GitHub repo.


What is the reason to do that?

Right now there are plenty of tutorials and articles about creating twitter bots out there, but I could not find any of them that extends the subject to the next level — offering direction for improving the way your bot handles the data.

That is why I decided to introduce a new player in the equation. Something that will help us to do more with our tweet reports — database.


MongoDB is a famous NoSQL database that has great integration with Node.js. It’s quick and easy to use, and it’s perfect for our purpose. We will use it to eliminate the duplicates from our feed, before sending the messages to the followers.

Step by step

If you want to follow through, here are the steps you may take:

  1. Register a new account in Mlab.
  2. Add your DBUSER and DBPASSWORD credentials to your .env file (same way as we did in the first part with the Twitter API keys).
  3. Create a new directory called “/db” in the root folder of your project and add a new file in it: db.js. Here is where we will put the logic for connecting with the database.
  4. At the beginning of the file, import mongoose — a MongoDB object modeling tool, designed to work in an asynchronous environment. We will see a bit later how easy is to work with MongoDB when using mongoose and Node.js.
const mongoose = require('mongoose')