Khoa Nguyen

All rights reserved © yunkhngn · v1.6.6

Journal

Engineering · 2026-11-22 · 3 min read

Building a Vector Search and Embeddings System with MongoDB

Explore how to integrate embeddings and Vector Search features directly into MongoDB to build intelligent semantic search applications.

Building a Vector Search and Embeddings System with MongoDB

On this page

  1. Introduction to Vector Search and Embeddings
  2. Overview of Vector Search Architecture
  3. Implementation with MongoDB Atlas
  4. Step 1: Data Preparation and Embedding Generation
  5. Step 2: Configuring the Atlas Search Index
  6. Step 3: Executing Vector Search Queries
  7. Key Considerations for System Optimization
  8. Conclusion
On this page
  1. Introduction to Vector Search and Embeddings
  2. Overview of Vector Search Architecture
  3. Implementation with MongoDB Atlas
  4. Step 1: Data Preparation and Embedding Generation
  5. Step 2: Configuring the Atlas Search Index
  6. Step 3: Executing Vector Search Queries
  7. Key Considerations for System Optimization
  8. Conclusion

Introduction to Vector Search and Embeddings

In the era of artificial intelligence (AI) and machine learning, modern applications increasingly require semantic understanding capabilities rather than traditional keyword-based search. Embeddings (vector representations) are the key to solving this problem.

Embeddings are arrays of floating-point numbers (vectors) representing text, images, or data in a multidimensional space. Objects with similar meanings are placed close to each other in this vector space. Vector Search allows us to find the nearest vectors based on mathematical distance (such as Cosine Similarity or Euclidean Distance).

Overview of Vector Search Architecture

To build a semantic search system, we need to convert raw data into vectors via an embedding model, then store and query them using a vector-supported database like MongoDB.

Flowchart
Loading diagram…
Photo by Domaintechnik on Unsplash

Implementation with MongoDB Atlas

MongoDB provides the Atlas Vector Search feature, allowing direct storage of vector fields within familiar JSON documents and efficient execution of k-NN (k-Nearest Neighbors) searches through the HNSW (Hierarchical Navigable Small World) algorithm.

Step 1: Data Preparation and Embedding Generation

Before storing in MongoDB, we need to use a model like OpenAI text-embedding-3-small or open-source models on Hugging Face to convert text into vectors.

{
  "_id": "ObjectId('...')",
  "title": "Giới thiệu về MongoDB",
  "content": "MongoDB là cơ sở dữ liệu NoSQL hướng tài liệu...",
  "embedding": [0.0123, -0.0456, 0.0789, "..."]
}

Step 2: Configuring the Atlas Search Index

On MongoDB Atlas, we define a Search Index with the data type set to vectorSearch to index the field containing vectors:

{
  "fields": [
    {
      "type": "vector",
      "path": "embedding",
      "numDimensions": 1536,
      "similarity": "cosine"
    }
  ]
}

Step 3: Executing Vector Search Queries

Use the Aggregation Pipeline with the $vectorSearch stage to perform the search:

const pipeline = [
  {
    $vectorSearch: {
      index: "vector_index",
      path: "embedding",
      queryVector: [0.0111, -0.0222, "..."],
      numCandidates: 100,
      limit: 5
    }
  },
  {
    $project: {
      title: 1,
      content: 1,
      score: { $meta: "vectorSearchScore" }
    }
  }
];

const results = await db.collection("articles").aggregate(pipeline).toArray();

Key Considerations for System Optimization

  • Dimensions: A larger number of dimensions may yield higher accuracy but comes at the cost of increased storage space and computation time.

  • NumCandidates: This parameter determines the number of candidate documents evaluated during approximate nearest neighbor (ANN) search. Balance is needed between latency and recall.

  • Hybrid Search: Combine traditional Full-Text Search and Vector Search to achieve optimal results.

Conclusion

Integrating Vector Search directly into MongoDB helps engineers simplify system architecture without needing to maintain an additional standalone vector database. This optimizes data management and system synchronization.

Keep exploring

From the journal

2026-11-16

Engineering Lawise: Building an AI-Powered LegalTech Platform

From legal documents to actionable intelligence — exploring the engineering behind an AI-powered legal platform built with Next.js, polyglot microservices, PostgreSQL, pgvector, and OpenAI.

2026-11-16

Building Distributed Systems: From Monolith to Microservices

An analysis of the challenges and lessons learned when migrating system architecture from a Monolith to Microservices within a distributed environment.

2026-11-15

AI Kitchen

Explore the building process of 'AI Kitchen' - a space where I turn raw ideas into complete products by optimizing my AI workflow.

Related work

2025

Yunkhngn Microservice Platform

A sophisticated microservice ecosystem powering a personal portfolio and interactive digital experiences, utilizing AI-driven image processing and a robust, containerized architecture.

2026

Dinner AI

An intelligent culinary web application powered by Google Gemini AI that automatically generates tailored dinner menus, recipes, and consolidated shopping lists.