Vector Search in MongoDB Atlas for RAG Applications
If your application data already lives in MongoDB, you can store embeddings in the same documents and query them with Atlas Vector Search, without running a separate vector database.
Store embeddings with your documents
await articles.insertOne({
title: 'Resetting your password',
body: 'Go to Settings, then Security...',
product: 'mobile-app',
embedding: await embed('Resetting your password. Go to Settings, then Security...'), // e.g. 1024 numbers
});
Create a vector search index
{
"fields": [
{ "type": "vector", "path": "embedding", "numDimensions": 1024, "similarity": "cosine" },
{ "type": "filter", "path": "product" }
]
}
Create it in the Atlas UI, the Atlas CLI or with the driver's createSearchIndex. The number of dimensions must match your embedding model.
Query with $vectorSearch
const queryVector = await embed('I forgot my login password');
const results = await articles.aggregate([
{
$vectorSearch: {
index: 'articles_vector',
path: 'embedding',
queryVector,
numCandidates: 200, // how many to consider (higher = better recall, slower)
limit: 5,
filter: { product: 'mobile-app' },
},
},
{ $project: { title: 1, body: 1, score: { $meta: 'vectorSearchScore' } } },
]).toArray();
Putting it into a RAG flow
- Embed the user's question with the same model used for the documents.
- Run
$vectorSearchwith filters for tenant, language or product. - Pass the top results as context to the LLM and ask it to answer from them.
Tips
- Store the embedding model name with each document so you know what to re-embed after a model change.
- Combine with Atlas Search (full-text) for hybrid keyword + semantic retrieval.
- Exclude the large
embeddingfield from results you send to clients.
Was this article helpful?
Comments
No comments yet. Have a question or a tip? Be the first to share it.