Vector Search in MongoDB Atlas for RAG Applications

A
Author
22 May, 2026 1 min read 1.16K views

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

  1. Embed the user's question with the same model used for the documents.
  2. Run $vectorSearch with filters for tenant, language or product.
  3. 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 embedding field 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.

Leave a comment

Comments are reviewed before they appear.