Text-to-SQL
Updated 14 October 20262 min read
Text-to-SQL is the task of turning a question written in plain language into a database query; the same idea applied to MongoDB produces a find() or an aggregation pipeline.
Text-to-SQL means converting a question in natural language, such as "which ten customers spent the most last quarter", into a query a database can run. Large language models made it practical: given the question and a description of the database, a model can write a SELECT with the right joins, filters and grouping. The same idea applied to MongoDB is often called text-to-MongoDB, and the output is a find() or an aggregation pipeline instead of SQL.
Schema context is what separates a working query from a plausible-looking one. A model that does not know the table and column names, their types and how tables relate will guess, and a guessed column name either fails or, worse, returns a wrong answer silently. Good systems give the model the schema, sample values for ambiguous columns and the relationships between tables or collections. For MongoDB this matters even more because there is no declared schema: field names, nested objects and types such as ObjectId versus string must be learned by sampling documents.
There are risks to manage. A model can write a query that modifies data, so production systems should connect with read-only credentials and also check the generated query before running it. Queries can be expensive, so timeouts and row limits protect the database. Questions can be ambiguous ("active users", "last month"), and a good assistant asks rather than guessing. Finally, every answer should show the query, so a person can verify how a number was produced.
Text-to-SQL tools fall into two groups. Query writers produce code for you to run yourself; they are useful for learning and for one-off queries. Assistants connect to the database, run the query and present the result as an answer, a table or a chart, which removes the copy and paste but raises the bar for safety.
wuup is the second kind for MongoDB: it scans your schema, writes a query, checks it against a read-only guard, runs it with limits and shows the query with the answer. Its free tools, such as the MongoDB query generator and the SQL to MongoDB converter, are the first kind and need no account.
Question: Revenue per status, highest first
SQL:
SELECT status, SUM(total) AS revenue
FROM orders
GROUP BY status
ORDER BY revenue DESC;
MongoDB:
db.orders.aggregate([
{ $group: { _id: "$status", revenue: { $sum: "$total" } } },
{ $sort: { revenue: -1 } }
])