Introduction

LLMs only know what they were trained on, so using them on your own data means retrieving relevant information and passing it to the model at query time. This pattern is Retrieval-Augmented Generation (RAG), and vector search powers it. If your data lives in MongoDB, MongoDB Atlas Vector Search lets you store embeddings beside your documents and query them through the aggregation framework.

What Is MongoDB Atlas Vector Search?

An embedding is a list of numbers representing the meaning of text, so similar texts sit close together in vector space. Atlas indexes embeddings stored in a normal document field and finds the nearest neighbors to a query vector. You define a vectorSearch index on that field, and Atlas keeps it synced. Because $vectorSearch is an aggregation stage, it combines with $match, $project, $lookup and others. Both approximate (ANN) and exact (ENN) search are supported.

Why Use Vector Search for LLM Applications?

It grounds answers in current data, enables semantic matching ("reset my login" finding "Recover your account access"), powers recommendations and deduplication, and gives agents long-term memory. Operational data, filters and vectors also live on one platform.

Choosing an Embeddings Approach

  • Atlas autoEmbed (Voyage AI): No embedding code; Atlas embeds your data and queries. Free tier available, fastest start.
  • sentence-transformers: Small Python helper. Free, runs on your own machine, suits full control and offline use.
  • OpenAI: Small Python helper and paid API credits. Suits teams already using OpenAI.

Automatic embedding is a newer feature, so check availability for your cluster tier and region.

Prerequisites

You need an Atlas account with a free M0 cluster (which supports Vector Search, with limits on search indexes), your IP on the network access list, and a database user. Path A needs a recent mongosh, and Path B needs Python 3.9 or later. No paid API key is required.

Path A: Automatic Embeddings with Voyage AI

  1. Create the index. Use createSearchIndexes with a vectorSearch index containing an autoEmbed field (on content, using the voyage-4 model) plus filter fields. No numDimensions or embedding code is needed.
  2. Wait for readiness. Check getSearchIndexes until queryable: true. The first build can take a minute or more.
  3. Search with plain text. Pass the question as query.text. For "how do I distribute data across servers?", Sharding ranked first (about 0.73), even though the query never uses the word "shard". Matching is based on meaning, not keywords.

Path B: Open-Source Embeddings with sentence-transformers

This path uses all-MiniLM-L6-v2 (384 dimensions), which runs on a laptop CPU.

  1. Generate and store embeddings. Encode each document's content and save the vector in an embedding field. Split longer documents into chunks of a few hundred tokens with slight overlap.
  2. Create the index. Define a vector field with 384 dimensions and cosine similarity, plus filter fields. numDimensions must match vector length exactly, filter fields must be declared as filter type, and builds are asynchronous, so wait until the index is queryable.
  3. Query. Embed the question with the same model as the documents and pass it as queryVector.

Sharding scored lower here (0.62), since small local models rank less precisely and Atlas rescales cosine scores to 0–1. For sharper ranking, ask more specific questions or switch to all-mpnet-base-v2 (768 dimensions), updating numDimensions and re-embedding.

Optional: OpenAI Embeddings

Only the embedding helper and index dimensions change. text-embedding-3-small returns 1536 dimensions by default, so set numDimensions accordingly in a new index. Vectors from different models can't be mixed, so re-embed if you switch. The API generally needs prepaid credits.

Common Problems and How to Fix Them

  • No results: confirm the index is READY and its name and path match your query.
  • Dimension mismatch: re-embed and update the index if you change models.
  • Filters not working: declare the field as a filter type and wait for the rebuild.
  • Irrelevant results: use the same model for queries and documents, then review chunk size, add filters and consider hybrid search.
  • Slow queries: review numCandidates, consider quantization, and check your tier's memory.
  • Maximum FTS indexes reached: free tiers allow few search indexes. Drop one or upgrade.
  • Empty results, no error: Atlas doesn't flag a missing index name, so check with getSearchIndexes().

Best Practices

Use one embedding model for documents and queries, chunk documents thoughtfully, declare filter fields early, keep secrets in environment variables, test retrieval on real questions before tuning the prompt, and monitor latency, index size and embedding costs.

Conclusion

You can build working semantic search on Atlas without paying for embeddings. Whether Atlas generates the vectors or you do, a vector index and one $vectorSearch stage deliver meaning-based results, which can combine with filters, hybrid search and an LLM for RAG. Start free, and move to a paid or larger model only if quality or scale demands it.