This article discusses new MongoDB Mastra integration features that simplify building retrieval-augmented generation (RAG) systems by consolidating previously disparate steps like embedding generation, hybrid search, and reranking into a single query. It highlights architectural improvements that eliminate the need for separate data copies and complex synchronization, thereby streamlining data retrieval workflows for AI agents built on MongoDB Atlas.
Read original on MongoDB BlogBuilding effective AI agents often relies on Retrieval-Augmented Generation (RAG), which involves retrieving relevant information from a data source to inform the language model. Traditionally, this process required a complex orchestration of steps: copying operational data, generating and syncing embeddings, performing separate semantic and keyword searches, merging results, reranking, and fetching full records. This fragmented approach led to significant maintenance overhead, potential data drift, and increased application-side complexity.
The new MongoDB Mastra integration (v1.28) introduces several features that significantly simplify the architecture of RAG systems by bringing these complex steps into a single, unified query within MongoDB Atlas. This consolidation reduces the burden on the application layer and leverages the database for more advanced search capabilities.
Impact on System Design
These features streamline the data pipeline for RAG, shifting responsibilities from the application and external services to the database. This leads to a more robust, easier-to-maintain, and performant architecture by reducing data movement, eliminating synchronization challenges, and centralizing complex search logic.
Consider an e-commerce agent answering a query like "What's the best laptop for video editing under $1,500?". Previously, this would involve multiple application steps. With the new integration, a single hybrid query can handle semantic matching for "video editing", apply filters for "under $1,500", and use native reranking to prioritize the best matches directly within MongoDB Atlas, simplifying the agent's logic.
const question = 'best laptop for video editing';
const recommendations = await store.hybridQuery({
indexName: 'productCatalog',
vector: { query: question, limit: 50 },
text: { query: question, paths: ['name', 'description'], limit: 50 },
rerank: { query: question, paths: ['name', 'description'], model: 'rerank-2.5', numDocsToRerank: 50 },
filter: { inStock: true, price: { $lt: 1500 } },
topK: 5,
});