Lesson 3.1:
Vector Search & RAG
Semantic Search with Vectors
Traditional keyword search is being replaced by semantic vector search. By converting text into high-dimensional vectors (embeddings), we can find records based on *meaning* rather than exact matches.
Atlas Vector Search Index
First, you must define a vector index in your Atlas cluster. This allows MongoDB to perform lightning-fast nearest-neighbor searches.
{
"fields": [
{
"type": "vector",
"path": "embedding",
"numDimensions": 1536,
"similarity": "cosine"
}
]
}
Retrieval-Augmented Generation (RAG)
RAG pipelines fetch relevant documents from your database and feed them to an LLM to provide context-aware, accurate answers. MongoDB Atlas serves as the "Long-Term Memory" for your AI agents.
// Example RAG flow
const context = await collection.aggregate([
{ $vectorSearch: { ... } }
]).toArray();
const response = await llm.complete({
prompt: `Use this context: ${JSON.stringify(context)}. Question: ${userQuery}`
});
Check Your Knowledge
Which similarity metric is most commonly used for text embeddings?