MongoDB vs. Elasticsearch
Jozsef Konnyu

MongoDB vs. Elasticsearch

What is MongoDB?

MongoDB is an open-source NoSQL database that uses a document-oriented data model.
This type of model is built on an architecture of collections and documents instead of using tables and rows like MySQL. Documents are built from key-value pairs which are the basic units of MongoDB. These documents may also be part of different collections - like tables - in a relational the database.

Being a NoSQL database MongoDB uses dynamic schemas for documents and as a result, they may be structurally different. This database also uses BSON (Binary JSON) – which is similar to JSON - to read the documents. Automatic sharing enables data in a collection to be distributed across multiple systems for horizontal scalability as data volumes increase.

What is Elasticsearch?

Elasticsearch is an open-source search engine which supports broad distribution and seamless scalability while ensuring enterprise quality. Data is accessible through an extensive and elaborate API – this way Elasticsearch can deliver extremely fast searches.

Fast, Incisive Search against Large Volumes of Data

Most SQL database management systems are not really prepared to handle full-text search, and as a result, they don't perform as well as search engines . For example, using the same hardware configuration, a query that takes 10 milliseconds in Elasticsearch, would take 10 seconds in MySQL.

Indexing Documents to the Repository

Elasticsearch uses a data handling format which is similar to JSON - just like MongoDB.
During an indexing operation, Elasticsearch converts raw data – such as log files or message files – into internal documents and stores it in indexes. Each document is a simple set of correlating keys and values.

Elasticsearch has a RESTful API which helps us to insert, update or delete our documents or indices, and to manage our cluster. MongoDB requires us to use one of the connector extensions of MongoDB - which depends on the programming language we use. If we use Elasticsearch, we will not necessarily need an extension – because the API is part of Elasticsearch – however, if we require more functionality, Elasticsearch provides official extensions for different languages.

Denormalized Document Storage: Fast, Direct access to your Data

It's important to keep in mind that Elasticsearch isn’t a relational database so we cannot use the well-known commands and functions from MySQL, such as join and subqueries. The most important concept that you must consider when switching from using conventional databases – is normalization.

ES will typically store a document once for each repository in which it resides. Although this is counterintuitive from the perspective of a MySQL database, it is optimal for ES. Full text searches will be extremely fast because the documents are stored in close proximity to the corresponding metadata in the index. This operation can reduce the number of data reads. In addition, ES decreases the index growth rate by keeping it compressed.

Broadly Distributable and Highly Scalable

We can scale up the Elasticsearch cluster to thousands of servers – and in addition, it can store petabytes of data. This giant capacity is a direct result of its elaborate distributed architecture, and scaling up a cluster is a very simple process.

These delicate and often intensive operations occur automatically and imperceptibly:

  • Distributing the documents to shards in a multi-node cluster
  • Balancing shards across all nodes in a cluster to evenly manage the indexing and search load
  • Redistributing shards to automatically recover from the loss of a node
  • Partitioning your documents across an arrangement of distinct shards (containers)
  • Routing requests from any node in the cluster to specific nodes containing the specific data that you need
  • Seamlessly adding and integrating new nodes as needed to increase the size of your cluster
  • Duplicating each shard to provide data redundancy and failover with replication

MongoDB or Elasticsearch?

While both of these options provide good solutions to typical MySQL problems – they are also significantly different. You need to keep in mind the fact that MongoDB is a NoSQL database system and Elasticsearch is a search engine, and one doesn’t replace the other. You can use only one of them or both of them.

Share your ideas with us about this article

Previous posts

Brand-new BitNinja WAF 2.0 is out now – open beta
Wooow! Are you ready for something new? Well, we have it!  The long-awaited BitNinja WAF 2.0 beta is now here! Currently, this beta is available for everyone who has Pro or Trial license. But wait! Before you go running to our Dashboard, to switch it on … please take a few minutes to read this article, as it contains lots of super valuable information – that will help familiarize you with this brand-new feature. Why WAF 2.0 is better than any other WAF solutions? •We grant a pre-defined, default ruleset for all the websites hosted on your server to guarantee low false po...
Suspect BitNinja behind increased server load?
As you know, providing all-in-one server security, BitNinja protects 3000+ Linux web servers worldwide, capturing 100 million incidents a month and keeping 1.7 million suspicious IP addresses in its blocklists to protect you and your customers. Mixing that up with custom infrastructures, configurations, and software in each leads to high load problems sometimes. Despite the best intentions - we always endeavor for the best solutions and we test our novelties constantly before and after each and every release -, these things happen. Most of them are temporary and everything falls back to nor...