In the previous article on RAG with Elasticsearch (https://4coders.own.pl/ai/rag-with-elasticsearch/), you detailed the full pipeline—ingestion, BM25/hybrid retrieval, and LLM integration—showing how Elasticsearch excels as a dedicated search engine for RAG apps. This follow-up assumes that foundation, shifting focus to MongoDB’s streamlined alternative via Atlas Vector Search, with a side-by-side comparison and thoughts on RAG’s evolution.
RAG in MongoDB: Quick Overview
MongoDB Atlas integrates RAG seamlessly through its Vector Search feature, storing documents with embeddings, metadata, and application data in a single collection for hybrid semantic and keyword queries. This simplifies ingestion (chunking data, generating embeddings) and retrieval, feeding relevant context directly to LLMs without separate storage layers.
Here’s a minimal example using MongoDB Node.js driver and OpenAI embeddings:
1. Create Vector Search Index (Atlas UI or CLI):
{
"fields": [
{
"numDimensions": 1536,
"path": "embedding",
"similarity": "cosine",
"type": "vector"
}
]
}2. Ingest Documents with Embeddings:
const { MongoClient } = require('mongodb');
const OpenAI = require('openai');
const client = new MongoClient('mongodb+srv://...');
const openai = new OpenAI({ apiKey: '...' });
async function ingest() {
await client.db('rag').collection('docs').insertOne({
content: "MongoDB Atlas Vector Search enables semantic search...",
metadata: { source: 'docs.pdf', page: 5 },
embedding: await openai.embeddings.create({
model: 'text-embedding-3-small',
input: "MongoDB Atlas Vector Search enables semantic search..."
}).then(res => res.data[0].embedding)
});
}3. Hybrid Vector + Keyword Retrieval:
async function retrieve(query) {
const queryEmbedding = await openai.embeddings.create({
model: 'text-embedding-3-small', input: query
}).then(res => res.data[0].embedding);
return await client.db('rag').collection('docs').aggregate([
{
$vectorSearch: {
queryVector: queryEmbedding,
path: "embedding",
numCandidates: 100,
limit: 5
}
},
{
$match: { "metadata.source": { $eq: "docs.pdf" } }
},
{
$project: { content: 1, score: { $meta: "vectorSearchScore" } }
}
]).toArray();
}For deeper dives, check the official MongoDB RAG course (https://learn.mongodb.com/learn/course/rag-with-mongodb-on-demand-marketing-webxp/rag-with-mongodb) and basics guide (https://www.mongodb.com/resources/basics/artificial-intelligence/retrieval-augmented-generation).[5]
Elasticsearch RAG vs MongoDB RAG
Elasticsearch demands more upfront work for advanced tuning like custom scorers and pipelines (as you implemented), suiting teams needing a dedicated search engine, but it adds stack complexity. MongoDB offers more “out of the box” for apps already on it—quick vector indexing with less glue code—ideal for simpler RAG prototypes.
Conclusions: Is RAG on the Right Path?
Both tools converge on hybrid search + vectors, making RAG a platform feature rather than a bespoke project (extending your ES example), which lowers barriers for adoption. However, “out of the box” ease risks black-box pitfalls: poor data quality or unmonitored retrieval can amplify LLM hallucinations, demanding rigorous evaluation regardless of stack. RAG’s direction feels positive – democratizing AI – but success hinges on treating it as an engineering discipline, not just a plug-in.
In the RAG arena, whether wielding Elasticsearch’s precision or MongoDB’s agility, true mastery lies in crafting retrieval that illuminates rather than overwhelms. Engineers: embrace the hybrid hunt, tune relentlessly, and let augmented intelligence propel your creations into uncharted frontiers where data meets destiny
