Elasticsearch to MongoDB Converter Online — Convert ES DSL to MongoDB Queries
Paste your Elasticsearch DSL query above and get the equivalent MongoDB query. Converts bool queries to MongoDB operators, aggregations to aggregate pipelines, all in your browser.
How to Convert Elasticsearch to MongoDB — Step-by-Step
Input Your Elasticsearch Query
Moving from Elasticsearch to MongoDB? Paste the _search body from Kibana or your code. You can include the GET /index/_search line and the converter will extract the index name to use as the MongoDB collection name.
.json fileExample: Elasticsearch Bool Query
An Elasticsearch query searching for active products priced at $50 or more in specific categories:
{ "query": { "bool": { "must": [ { "term": { "status": "active" } }, { "range": { "price": { "gte": 50 } } }, { "terms": { "category": ["electronics", "accessories"] } } ], "must_not": [ { "term": { "discontinued": true } } ] } }, "sort": [{ "price": "desc" }], "size": 20 }
Automatic Conversion
The converter maps Elasticsearch query types to MongoDB operators:
term → equality, terms and ids → $in, range → $gt/$gte/$lt/$lte, exists → $exists (and not null)match / match_phrase → case-insensitive $regex, prefix and wildcard → anchored $regex with special characters escapedbool.must and filter → all must match, should → $or (only when there is no must or filter, or with minimum_should_match), must_not → $norsort → .sort(), from → .skip(), size → .limit() (10 when not set, countDocuments() for size 0), _source → projectionResult: MongoDB find() Query
The bool query above converts to this MongoDB equivalent:
// Note: No index name was given, so the collection is called "collection". Add a line like GET /products/_search to name it.
db.collection.find(
{
"status": "active",
"price": {
"$gte": 50
},
"category": {
"$in": [
"electronics",
"accessories"
]
},
"$nor": [
{
"discontinued": true
}
]
}
).sort({ "price": -1 }).limit(20)Aggregation Conversion
Elasticsearch aggregations with terms buckets and sub-aggregations are converted to MongoDB aggregate() pipelines using $group stages. Metrics like sum, avg, min, max, and value_count map directly to their MongoDB equivalents.
Input: Elasticsearch Terms Aggregation with Sub-Aggs
Group orders by status and calculate total revenue and average amount per group:
{ "size": 0, "aggs": { "by_status": { "terms": { "field": "status" }, "aggs": { "total_revenue": { "sum": { "field": "amount" } }, "avg_amount": { "avg": { "field": "amount" } } } } } }
Output: MongoDB aggregate() Pipeline
The aggregation becomes a pipeline that unwinds the field, groups with $group, sorts by count and keeps the top 10 buckets, as Elasticsearch does when no size is set (a note line about that and the collection name sits above it):
db.collection.aggregate([
{
"$unwind": "$status"
},
{
"$group": {
"_id": "$status",
"doc_count": {
"$sum": 1
},
"total_revenue": {
"$sum": "$amount"
},
"avg_amount": {
"$avg": "$amount"
}
}
},
{
"$sort": {
"doc_count": -1,
"_id": 1
}
},
{
"$limit": 10
}
])Copy or Download
Once the conversion is complete, grab the MongoDB output and use it immediately in your preferred tool or workflow.
.js for use in scripts and CI/CD pipelinesFrequently Asked Questions
Which Elasticsearch queries can it convert?
term, terms, ids, exists, range (including dates and now-7d style date math), prefix, wildcard, regexp, match, match_phrase, multi_match, bool, constant_score, dis_max and nested (written as $elemMatch). Anything else, such as fuzzy, query_string, geo or script queries, is not guessed at: it is left out and a "// Note:" line at the top of the output names it, because a missing clause changes which documents come back.
How are bool queries translated?
Every must and filter clause has to match, so they are combined into one filter. must_not becomes $nor, which leaves out a document if any of those clauses matches. should becomes $or only when the bool has no must or filter; next to must or filter, Elasticsearch uses should just for scoring, so it is left out unless you set minimum_should_match. A minimum_should_match of 2 or more becomes an $or of the possible combinations.
Why does match turn into a regex?
A MongoDB find() has no text analyzer, so each word of a match query becomes a case-insensitive "contains" regex: any word with the default OR operator, every word with "operator": "and". match_phrase uses the whole phrase. Regex characters are escaped, so "v1.0 (beta)" means exactly that text. A regex can also match inside a longer word, which Elasticsearch would not do; for real word matching, add a text index and use $text.
How do sort, size, from, _source and aggregations map?
sort becomes .sort() (sorting by _score has no MongoDB equivalent and is noted), from becomes .skip() and size becomes .limit(). With no size you get .limit(10), because Elasticsearch returns 10 hits by default, and size 0 becomes countDocuments(). _source becomes the projection. A terms aggregation becomes an aggregate() pipeline with $unwind, $group (doc_count plus sum, avg, min, max, value_count or cardinality), a sort by count and a limit of size, 10 when not set. Several aggregations run side by side in $facet; date histograms and other bucket types are named in a note.
Are long IDs and decimals kept exactly?
Yes. Numbers JavaScript can hold are written as typed, so 14.50 stays 14.50. Bigger integers, such as an ICCID like 8901240544102066246, become NumberLong("8901240544102066246") so mongosh keeps every digit, and values too precise for a double become NumberDecimal. Quoted values like "00501" stay strings.
Can I paste straight from Kibana Dev Tools?
Yes. A GET or POST line for _search or _count is read, and the index becomes the collection name. Names mongosh would read as one of its own methods (stats, version) or names with dashes are written as db.getCollection("..."). Lines starting with # or // are skipped, while the same characters inside a string value are kept. If you paste several requests, only the first is converted, and .keyword sub-fields are mapped to the field itself.
Is my query sent anywhere?
No. The conversion runs in your browser and nothing is uploaded or kept; reloading the page starts with an empty editor. There is no account to create.
Related converters: MongoDB to Elasticsearch, SQL to MongoDB, Elasticsearch to SQL and Elasticsearch Query Formatter.
Related Tools
SQL to MongoDB
Convert SQL queries to MongoDB query syntax with aggregation pipeline support
MongoDB to SQL
Convert MongoDB queries and aggregation pipelines to equivalent SQL statements
SQL to DynamoDB
Convert SQL queries to AWS DynamoDB query and scan operations with PartiQL support
DynamoDB to SQL
Convert AWS DynamoDB query and scan operations to equivalent SQL statements
MongoDB Query Formatter
Format, beautify, and validate MongoDB queries with proper indentation and syntax highlighting
MongoDB to DynamoDB
Convert MongoDB queries to AWS DynamoDB query operations and vice versa