designwithfocus

Data & databases

Elasticsearch vs MongoDB: Search Index or Documents

By Design With Focus Team 8 min read
A search index card beside a document record card with a few fields

Short answer

Use Elasticsearch when the job is a search index over text, and MongoDB when the job is the application document. Elastic's docs center on an index, Lucene shards, and an inverted index scored with BM25. MongoDB's docs center on BSON documents in collections. MongoDB also lists full-text search as one workload.

Last verified against the sources listed below.

Key takeaways

  • An Elasticsearch index is the storage unit you add documents to and search.
  • Full-text search there builds an inverted index and scores matches with BM25.
  • A MongoDB record is a BSON document. The maximum size is 16 mebibytes.
  • Collections are similar to tables but do not enforce a rigid schema.
  • Elastic Cloud Serverless projects document a cap of 15,000 indices.

Elasticsearch vs MongoDB comes down to the object you are building. Choose Elasticsearch for a search index. Choose MongoDB for the document that is the application record.

Elastic’s index guide calls an index the fundamental unit of storage and the level at which you interact with data. You add a document to an index to store it. You target one or more indices to search.

MongoDB’s introduction calls MongoDB a document database that stores flexible, JSON-like documents. Both products store records that look like objects. The docs put the weight in different places.

Elasticsearch MongoDB
Record JSON document in an index BSON document in a collection
Search structure Inverted index, scored with BM25 Query API, including full-text search as a listed workload
Shard or scale unit Primary shards are Lucene indexes, fixed at creation Sharding partitions by a shard key
Document cap named in the docs Not the 16 mebibyte BSON cap 16 mebibytes per BSON document
Transactions Not the subject of the index fundamentals page Multi-document ACID transactions

What is an Elasticsearch index?

It is the named collection of JSON documents you write to and search, split behind the scenes into shards.

Each document is a set of fields, key-value pairs, plus system metadata such as _index and _id. You can set the id or let Elasticsearch generate it. A mapping on the index sets each field’s type and how it is indexed.

Settings cover shard count, replica count, and refresh interval. index.number_of_shards cannot be changed after the index is created. index.number_of_replicas can change later.

Each shard is a self-contained Apache Lucene index. Lucene’s project page describes Lucene Core as a Java search library with indexing, search, spellchecking, hit highlighting, and analysis. As of December 2026, that page lists Apache Lucene 10.6.0.

You do not query shards by hand. You name the index, and Elasticsearch routes the operation.

Elastic Cloud Serverless, on the same index page, says shards, replicas, and nodes are managed for you, and that each project supports up to 15,000 total indices. That cap is for Serverless projects, not a universal Elasticsearch limit. The page says you can request a higher limit.

Index mode changes storage and queries. Standard mode is described as optimized for search and any type of retrieval. Other modes on the page target logs, metrics, vectors, or lookup data.

The mode is fixed when the index is created.

How does full-text search work there?

Text is analyzed into tokens, those tokens fill an inverted index, and a query searches that index. Results carry a _score.

Elastic’s full-text guide lists four parts. Analysis is a pipeline, with stemming, lowercasing, and stop-word removal among the examples. Elasticsearch ships analyzers and lets you define custom ones.

After analysis, it builds an inverted index that maps each token to the documents that contain it. That structure has a dictionary of terms and, per term, a posting list of document ids, optionally with term frequency and position.

The default similarity is Okapi BM25, which the page calls a variation of TF-IDF. BM25 uses term frequency, document frequency, and document length. The score is a positive floating-point number.

A higher _score means a more relevant document, in that ranking.

The query text is analyzed the same way as the indexed text. Query DSL has full-text queries. The page says that as of 8.17, ES|QL also supports full-text search functions.

A text mapping is what makes a field take that path. A keyword mapping, in the index guide’s example, is a different type. Pick the type before production.

The same page says changing a field type later requires reindexing.

What is a MongoDB document?

It is the basic unit of data: field-and-value pairs stored as BSON, kept in a collection.

The document page says BSON is a binary representation of JSON with more data types than JSON, and it points to the BSON spec. bsonspec.org says BSON supports embedded documents and arrays, plus types JSON does not have, such as Date and BinData. It lists three design goals: lightweight, traversable, and efficient encoding.

Traversability is tied there to MongoDB’s use of BSON as its primary data representation.

A sample document on MongoDB’s page uses an ObjectId for _id, an embedded name, Date fields, an array of strings, and a Long. The _id is the primary key in a standard collection. It must be unique, it is immutable, and it cannot be an array or a regex.

If you omit it on insert, the driver or server adds an ObjectId. The server keeps _id as the first field.

The maximum BSON document size is 16 mebibytes. Larger payloads are what the page says GridFS is for. Field names must be unique within the document.

Duplicate fields make CRUD behavior unexpected, and the query language does not support them. Starting in MongoDB 6.1, the validate command with full set to true can check for duplicate field names.

The introduction says collections are similar to relational tables but do not enforce a rigid, pre-defined schema. Embedded documents and arrays are how the manual says you reduce joins. Dynamic schemas allow different shapes in one collection.

Multi-document transactions provide atomicity, consistency, isolation, and durability, including across a sharded cluster. Replication provides automatic failover. Sharding partitions data by a shard key.

The introduction also lists workload types: document-based structured search, aggregation, full-text search, vector search, geospatial search, and time series. Full-text search is on MongoDB’s list. It is not absent.

The product page that explains the inverted index and BM25, in the sources above, is Elastic’s.

How a document database differs from other models is covered in the document database guide.

Tables, rows, and joins, as a separate model, are the subject of the relational database guide.

Which should you choose?

Choose Elasticsearch when the primary question is “which documents match this text, and in what order?” Choose MongoDB when the primary question is “store this record, update it, and read it back in the shape the application uses.”

Signs the index is the right center:

  • You need analysis, an inverted index, and a relevance _score.
  • Mappings should freeze field types, with reindexing as the way to change them.
  • The storage unit you want to name in queries is an index, not a table of rows.

Signs the document database is the right center:

  • Records are objects with embedded fields and arrays, and they change in place.
  • You want multi-document ACID transactions and replica-set failover as described in MongoDB’s introduction.
  • A 16 mebibyte document cap fits the record, or large binaries go to GridFS.

You can use both. An application can keep the record in MongoDB and send the text that must be searched to an Elasticsearch index. This guide does not invent a speed winner.

Neither set of docs publishes a shared benchmark.

If a document includes an image file, compress it before you store the bytes beside the record. An image compressor does that in the browser. The database choice stays the same.

Only the file size changes.

What should you decide before the first write?

Decide the unit, the identifier, and what you are allowed to change later.

For Elasticsearch, name the index, set explicit mappings for production, and set index.number_of_shards knowing it is fixed. Replica count can wait. Standard mode is the search-oriented mode on the index page.

If the data is append-only logs or metrics, the same page points at data streams and other index modes instead of a regularly updated index.

For MongoDB, decide which fields are embedded and which stay in another collection. Embedding is how the introduction says you avoid joins. The 16 mebibyte cap is the check on a single document.

Decide the _id. An ObjectId is the default the document page describes. A natural unique id is the alternative it lists when you already have one.

Write a sample document and a sample query before you pick. If the query is “rank these passages,” read the full-text page and the mapping types. If the query is “load this user’s order with its line items,” read the document model.

The verdict at the top is that split, and the tables in the vendor docs are the source of it.

Frequently asked questions

Does MongoDB store JSON or BSON?

MongoDB's document page says it stores data records as BSON documents. BSON is a binary representation of JSON documents, with more data types than JSON. The BSON specification describes the same idea: a binary-encoded serialization of JSON-like documents, including a Date type and a BinData type, and it names traversability for MongoDB as a design goal. You can still think of the record as JSON-like. The on-disk and on-the-wire form the manual names is BSON, not a plain text JSON file.

What is the 16 mebibyte limit?

MongoDB's document page says the maximum BSON document size is 16 mebibytes. The stated reason is to keep one document from using an excessive amount of RAM or bandwidth. For larger files, the same page says MongoDB provides the GridFS API. That limit is about one document, not about the whole database. A collection can hold many documents. If a record would embed a large video, the manual points you at GridFS instead of a single document.

Can you change the number of primary shards later?

Elastic's index fundamentals page says the number of primary shards is fixed at index creation, through an index template or the index.number_of_shards setting. Every document belongs to exactly one primary shard. Replica shards are copies you can add or remove later with index.number_of_replicas. They add redundancy and can serve reads. Plan the primary count before you create the index. The page also says there is no single correct shard count for every dataset.

Does Elasticsearch only store text?

No. Documents are JSON objects, fields are key-value pairs, and mappings set a type per field. Elastic's example mapping uses keyword for an email, text for a name, integer for an age, and date for a join date. Text fields are the ones that go through analysis into an inverted index. Keyword, numbers, and dates are still part of the document. Standard index mode, on that page, is described as optimized for search and any type of retrieval.

Which one replaces a relational database?

Neither page says it replaces every relational database. MongoDB's introduction says a document can mirror application objects and that embedded documents and arrays reduce the need for joins. It also says collections are similar to relational tables but do not enforce a rigid schema. Elasticsearch is an index you search, divided into Lucene shards, not a table of rows. If the system of record is rows with constraints, read the relational database guide before you pick either product as the only store.

Sources