Elasticsearch vs MongoDB: search engine compared with document database

Elasticsearch vs MongoDB: Which One to Use and When

On the surface, Elasticsearch and MongoDB look alike. Both store JSON-style documents made of key-value pairs, and both let you query them. But they come from two different camps and are built for different jobs.

MongoDB is a general-purpose document database. Elasticsearch is a distributed search engine built on Apache Lucene. Elasticsearch is very good at one job: indexing and searching big datasets fast. Because its design is tuned for that, it is weaker at other things. Writes are heavier than in most NoSQL databases, because every document has to be analysed and indexed before it shows up in search.

Quick answer

  • Use MongoDB as the main database for your app data: users, orders, carts, content.
  • Add Elasticsearch when you need a real search box (typo tolerance, synonyms, relevance ranking), faceted filters, or log analytics on top of that data.
  • Small app, simple search? MongoDB alone may be enough, using text indexes or Atlas Search if you are on MongoDB Atlas.

Elasticsearch vs MongoDB at a glance

ElasticsearchMongoDB
What it isDistributed search and analytics engineGeneral-purpose document database
Built onApache LuceneIts own storage engine (WiredTiger by default)
Data formatJSON documentsBSON documents
Best atFull-text search, relevance ranking, filters, aggregations, logsStoring and updating app data, CRUD, transactions
TransactionsNo multi-document transactionsMulti-document ACID transactions (since 4.0)
When new data is visibleNear real-time, after an index refresh (1 second by default)Right after the write
How you queryREST API with JSON Query DSLMongoDB Query API and aggregation pipeline, drivers for most languages
ScalingShards and replicasReplica sets and sharding
Full-text searchCore strength: analyzers, fuzzy matching, synonyms, scoringBasic text indexes; Lucene-based Atlas Search on MongoDB Atlas
LicenseElastic License 2.0, SSPL or AGPLv3 (AGPL option added in 2024)SSPL for the Community edition

Where Elasticsearch wins

  • Search boxes. Typo tolerance, synonyms, autocomplete and “did you mean” are built in. Results are ranked by relevance (BM25 scoring by default), not just matched.
  • Faceted filters. Brand, price range, rating and the count next to each filter can all come back from one query using aggregations. This is what makes e-commerce listing pages feel fast.
  • Logs and metrics. The ELK stack (Elasticsearch, Logstash, Kibana) is a standard way to collect and search logs. Append-only data at high volume suits it well.

Where MongoDB wins

  • Source of truth. Your users, orders and payments should live in a database built for durability and correctness.
  • Frequent updates. Lucene segments are immutable, so an update in Elasticsearch is a delete plus a re-index of the whole document. MongoDB updates fields in place.
  • Transactions. Placing an order and reducing stock in one all-or-nothing step is a MongoDB job, not an Elasticsearch one.
  • Changing schema. Early-stage products change their data shape every week. MongoDB is relaxed about that. In Elasticsearch, changing the type of an existing field means re-indexing.

How we used Elasticsearch at Mebelkart

In practice, Elasticsearch is usually used next to a SQL or NoSQL database. The database is the persistent store, and Elasticsearch handles the complex search queries on the content. At Mebelkart we ran Elasticsearch on top of MySQL for the full product catalogue, sellers, product reviews and categories. All our heavy read queries and all full-text search went to Elasticsearch indexes. If you are building an app with lots of filters and search, Elasticsearch (or Solr) is the right tool for that layer.

You can use Elasticsearch as a primary store, but it works best next to a persistent database like MongoDB or MySQL. The database gives you constraints, correctness and durability, and you keep pushing data to Elasticsearch on every add or update.

How to keep MongoDB and Elasticsearch in sync

  • Change streams. MongoDB (3.6 and later) can stream every insert, update and delete. A small worker listens and writes the change to Elasticsearch.
  • Monstache. An open-source Go daemon that does the change-stream sync for you, with config for which collections go to which index.
  • Kafka Connect. If you already run Kafka, the Debezium MongoDB source connector plus an Elasticsearch sink connector gives you a durable pipeline.
  • Dual writes from app code. The simplest option, but the two can drift when one write fails. Put the Elasticsearch write behind a queue with retries.

One habit that saved us more than once: treat the search index as something you can throw away. Keep a script that rebuilds the whole index from the database, and run it whenever the mapping changes or the two drift apart.

Do you even need both?

  • MongoDB Atlas Search runs Lucene-based full-text search inside Atlas, so there is no second cluster to sync. A good fit if you are already on Atlas.
  • MongoDB text indexes cover basic keyword search. Fine for an admin panel or a small app, but there is no fuzzy matching.
  • Elasticsearch on its own is fine for logs and analytics, where data is append-only and the original lives somewhere else.
  • OpenSearch is the community fork of Elasticsearch 7.10 under the Apache 2.0 license. Worth a look if licensing matters to you.

FAQ

Is Elasticsearch a database?

It stores data and you can query it, so it can act like one. But it is built as a search engine: no multi-document transactions, and new data shows up after a short refresh. Most teams keep a primary database and use Elasticsearch for search.

Is Elasticsearch faster than MongoDB?

For full-text search and aggregations over large data, usually yes. For simple lookups by ID and frequent writes or updates, MongoDB is usually the better fit.

Can MongoDB replace Elasticsearch?

For basic search, yes, with text indexes or Atlas Search. For a heavy search experience with custom analyzers, relevance tuning or large log volumes, Elasticsearch is still the stronger choice.

More on picking a stack: Tech stack and architecture.

Similar Posts