From a mongosh query to an Elasticsearch search request
Paste a MongoDB query above and you get the Elasticsearch query DSL that asks for the same documents: filters become a bool query, and a $group pipeline becomes aggregations. Anything that can't be expressed the same way is listed in a note above the request instead of being dropped quietly.
How to Convert MongoDB to Elasticsearch, Step by Step
Paste a MongoDB query
Moving search traffic from MongoDB to Elasticsearch? Paste the query exactly as you run it in mongosh or Compass. Shell syntax works as is: unquoted keys, single quotes, comments, /regex/i, ObjectId() and ISODate(). A bare filter document such as { status: "active" } works too.
find(), findOne(), aggregate(), countDocuments(), distinct().js, .json or .txt file$in, a case-insensitive regex, $ne, a projection, sort and limitExample: a find() with several operators
db.products.find({
status: "active",
price: { $gte: 50, $lte: 500 },
tags: { $in: ["electronics", "sale"] },
name: { $regex: "^Pro" }
}).limit(20)Read the converted request
Each query operator is mapped to the clause that returns the same documents, and every condition goes into a non-scoring filter, just as a MongoDB filter doesn't rank results.
$gt/$gte/$lt/$lte → one range, $in → terms, $ne/$nin → must_not$regex → regexp (unanchored patterns get .* on both sides), or prefix for a plain ^text$and, $or, $nor → bool filter, should, must_not.sort() → sort, .skip() → from, .limit() → sizeOutput for the example above
# Note: term, terms, prefix and regexp compare the exact stored value. For text fields use the keyword sub-field (for example "name.keyword").
GET /products/_search
{
"query": {
"bool": {
"filter": [
{
"term": {
"status": "active"
}
},
{
"range": {
"price": {
"gte": 50,
"lte": 500
}
}
},
{
"terms": {
"tags": [
"electronics",
"sale"
]
}
},
{
"prefix": {
"name": "Pro"
}
}
]
}
},
"size": 20
}Check field mappings
MongoDB compares strings exactly, and so do term, prefix and regexp, but only on keyword fields. If your index maps name as text, change the field to name.keyword. A $group pipeline turns into a terms aggregation, and a $sort on one of its results orders the buckets:
Example: revenue per category
db.orders.aggregate([
{ $match: { status: "completed" } },
{ $group: {
_id: "$category",
totalRevenue: { $sum: "$amount" },
avgOrder: { $avg: "$amount" }
} },
{ $sort: { totalRevenue: -1 } }
])Output
# Note: Elasticsearch returns at most "size" buckets (set to 10,000 here). For more groups, page with a composite aggregation.
# Note: term, terms, prefix and regexp compare the exact stored value. For text fields use the keyword sub-field (for example "name.keyword").
GET /orders/_search
{
"query": {
"bool": {
"filter": [
{
"term": {
"status": "completed"
}
}
]
}
},
"size": 0,
"aggs": {
"by_category": {
"terms": {
"field": "category",
"size": 10000,
"order": {
"totalRevenue": "desc"
}
},
"aggs": {
"totalRevenue": {
"sum": {
"field": "amount"
}
},
"avgOrder": {
"avg": {
"field": "amount"
}
}
}
}
}
}Copy or download
The output is in Kibana Dev Tools format, so you can paste it straight into the console; the # lines are comments there. For curl or an Elasticsearch client, use the JSON body below the GET line. Download saves the same text as elasticsearch-query.txt.
Frequently Asked Questions
Which MongoDB queries can it convert?
Read queries written the way you type them in mongosh: db.collection.find() with a projection and chained .sort(), .skip() and .limit(), findOne(), countDocuments(), count(), estimatedDocumentCount(), distinct() and aggregate(). Unquoted keys, single quotes, comments, trailing commas, /regex/i literals and helpers such as ObjectId(), ISODate(), new Date() and NumberLong() are all understood. Writes like insertOne() or updateMany() are refused, because a search request can't change data.
How are the MongoDB operators translated?
A plain value or $eq becomes a term query, $gt/$gte/$lt/$lte become one range query, $in becomes terms and $nin a must_not around terms. $ne becomes must_not, $exists becomes exists, and field: null becomes "field is missing". $regex and /regex/ literals become a regexp query (or a prefix query for a plain ^text pattern), with $options: "i" turned into case_insensitive. $and, $or and $nor become bool filter, should and must_not. $all becomes one term per value, and $elemMatch becomes a nested query, which needs the array mapped as nested in Elasticsearch.
What happens to operators that have no Elasticsearch equivalent?
They are never dropped silently. $where, $expr, $text, $size, $type, $mod, geo operators, regex features Elasticsearch lacks (such as \b or lookarounds) and pipeline stages like $lookup or $unwind are each named in a "# Note:" line above the request. Leaving a condition out means the search can return more documents than MongoDB would, so read the notes before running it.
Does it convert aggregation pipelines?
The common shape, yes. $match stages become the query, and a $group on a field becomes a terms aggregation with sum, avg, min and max sub-aggregations for $sum, $avg, $min and $max. A { $sum: 1 } count is the doc_count Elasticsearch already returns for each bucket. A $sort after $group orders the buckets and a $limit sets how many come back. A compound _id becomes a multi_terms aggregation, and _id: null computes the metrics over every matching document.
Why do my text fields need .keyword?
term, terms, prefix and regexp compare the exact stored value, just like a MongoDB equality check. On a text field Elasticsearch stores analysed, lowercased tokens, so an exact match on "Wireless Mouse" finds nothing. Point those clauses at the keyword sub-field (name.keyword) or map the field as keyword. The output reminds you whenever a query compares strings.
Are long IDs and decimals kept exactly?
Yes. The JSON body is written straight from the text of your query, so an ICCID like 8901240544102066246 stays a bare number with every digit, NumberLong("-9223372036854775808") comes out as that number, 14.50 stays 14.50, and a quoted "00501" stays a string.
Can I paste the output into Kibana?
Yes. The result is written in Kibana Dev Tools format: an optional "# Note:" comment block, a GET /index/_search (or _count) line, and the JSON body. The collection name becomes the index name, lowercased if needed, since Elasticsearch only accepts lowercase index names. Download saves the same text as a .txt file.
Is my query sent anywhere?
No. The conversion runs in your browser, the query is not uploaded, and nothing is kept: a reload starts with an empty editor.
Going the other way? Try Elasticsearch to MongoDB. To tidy a query before converting it, use the MongoDB 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