Introducing Text-to-MQL with LangChain: Query MongoDB using Natural Language

We’re excited to announce that we’ve added a powerful new capability to the MongoDB integration for LangChain: Text-to-MQL. This enhancement allows developers to easily transform natural language queries into MongoDB Query Language (MQL), enabling them to build new and intuitive application interfaces powered by large language models (LLMs).

Whether you’re building chatbots to interact with internal company data stored on MongoDB or AI agents that will work directly with MongoDB, this LangChain toolkit delivers out-of-the-box natural language querying with Text-to-MQL.

Enabling new interfaces with Text-to-MQL

LLMs are transforming the workplace by enabling people to “talk” to their data. Historically, accessing and querying databases required specialized knowledge or tools. Now, with natural language querying enabled by LLMs, developers can create new, intuitive interfaces that give virtually anyone access to data and insights—no specialized skills required.

Using Text-to-MQL, developers can build applications that rely on natural language to generate insights or create visualizations for their users. This includes conversational interfaces that query MongoDB directly, democratizing database exploration and interactions.

Robust database querying capabilities through natural language are also critical for building more sophisticated agentic systems. Agents leveraging MongoDB through MQL can interact autonomously with both operational and analytical data, greatly enhancing productivity across a wide range of operational and business tasks.

Figure 1. Agent components and how MongoDB powers tools and memory.
Diagram showing the agent components and how MongoDB powers tools and memory. At the top is Agents, which has a line going down that connects to four boxes. On the left, is a box labeled perception with sub-boxes for text, audio, images, and multimodal. The next box is planning, with sub-boxes for reflexion, chain of thought, decompose, and reAct. The third box is Tools, with sub-boxes for RAG, Text-to-MQL, code interpreter, and web search. The fourth box is memory, which contains short-term and long-term memory.

For instance, customer support agents leveraging Text-to-MQL capabilities can autonomously retrieve the most recent customer interactions and records directly from MongoDB databases, enabling faster and more informed responses. Similarly, agents generating application code can query database collections and schemas to ensure accurate and relevant data retrieval logic.

In addition, MongoDB’s flexible document model aligns more naturally with how users describe data in plain language. Its support for nested, denormalized data in JSON-like BSON documents reduces the need for multi-table joins—an area where LLMs often struggle—making MongoDB more LLM-friendly than traditional SQL databases.

Implementing Text-to-MQL with MongoDB and LangChain

The LangChain and MongoDB integration package provides a comprehensive set of tools to accelerate AI application development. It supports advanced retrieval-augmented generation (RAG) implementations through integrations with MongoDB for vector search, hybrid search, GraphRAG, and more. It also enables agent development using LangGraph, with built-in support for memory persistence. The latest addition, Text-to-MQL, can be used either as a standalone component in your application or as a tool integrated into LangGraph agents.

Figure 2. LangChain and MongoDB integration overview.
Diagram showing the overview of the LangChain and MongoDB integration overview. The title of the diagram is ship AI applications faster with MongoDB and LangChain. On the left is a label for RAG, which runs down and connects to vector search, semantic cache, graphRAG, advanced retail, indexing & tracking, and chat history. On the right, is agents, which goes down and connects to shor-term memory, long-term memory, and text-to-MQL.

Released in version 0.6.0 of the langchain-mongodb package, the agent_toolkit class introduces a set of methods that enable reliable interaction with MongoDB databases, without the need to develop custom integrations.

The integration enables reliable database operations, including the following pre-defined tools:

  • List the collections in the database

  • Retrieve the schema and sample rows for specific collections

  • Execute MongoDB queries to retrieve data

  • Check MongoDB queries for correctness before executing them

You can leverage the LangChain database toolkit as a standalone class in your application to interact with MongoDB from natural language and build custom text interfaces or more complex agentic systems. It is highly customizable, providing the flexibility and control needed to adapt it to your specific use cases. More specifically, you can tweak and expand the standard prompts and parameters offered by the integration.

When building agents using LangGraph—LangChain’s orchestration framework—this integration serves as a reliable way to give your agents access to MongoDB databases and execute queries against them.

Real-world considerations when implementing Text-to-MQL

Natural language querying of databases by AI applications and agentic systems is a rapidly evolving space, with best practices still taking shape. Here are a few key considerations to keep in mind as you build:

Ensuring accuracy

The generated MongoDB Query Language (MQL) relies heavily on the capabilities of the underlying language model and the quality of the schema or data samples provided. Ambiguities in schemas, incomplete metadata, or vague instructions can lead to incorrect or suboptimal queries. It’s important to validate outputs, apply rigorous testing, and consider adding guardrails or human review, especially for complex or sensitive queries.

Preserving performance

Providing AI applications and agents with access to MongoDB databases can present performance challenges. The non-deterministic nature of LLMs makes workload patterns unpredictable. To mitigate the impact on production performance, consider routing agent queries to a replica set or using dedicated, optimized search nodes.

Maintaining security and privacy

Granting AI apps and agents access to your database should be considered with care. Apply the common security principles and best practices: define and enforce roles and policies to implement least-privilege access, granting only the minimum permissions necessary for the task. Giving access to your data may involve sharing private and sensitive information with LLM providers. You should evaluate what kind of data should actually be sent (such as database names, collection names, or data samples) and whether that access can be toggled on or off to accommodate users.

Build reliable AI apps and agents with MongoDB

LLMs are redefining how we interact with databases. We’re committed to providing developers the best paths forward for building reliable AI interfaces with MongoDB. We invite you to dive in, experiment, and explore the power of connecting AI applications and agents to your data. Try the LangChain MongoDB integration today!

Ready to build? Dive into Text-to-MQL with this tutorial and get started building your own agents powered by LangGraph and MongoDB Atlas!

This article first appeared on Read More