Vector Databases Explained

Article

Vector Databases Explained

Published on
Authors

What’s the Problem with Regular Search?

  • You have 10,000 product descriptions
  • User types: “comfortable outdoor furniture”
  • Traditional database (SQL/NoSQL):
    • Looks for exact words: “comfortable” OR “outdoor” OR “furniture”
    • Misses “cozy patio seating” — even though it’s the same thing
    • Keyword matching = dumb

Vector databases fix this with meaning, not just words.


So, What Is a Vector Database?

A vector database stores numbers that represent meaning, not just text.

Regular DatabaseVector Database
Stores: "cozy patio seating"Stores: [0.3, 0.8, 0.1, 0.9, ...]
Searches: exact wordsSearches: similar meanings
  • These numbers = embeddings (created by AI models like OpenAI, Google, etc.)
  • Similar ideas → similar numbers
  • “Cozy” and “comfortable” → close numbers
  • “Chair” and “table” → far apart

How Does It Work? (Step-by-Step)

Step 1: Turn Text into Numbers (Vectors)

flowchart LR
    T1["'comfortable chair'"] --> V1["[0.2, 0.7, 0.1, 0.4, ...]"]
    T2["'cozy seat'"] --> V2["[0.3, 0.8, 0.2, 0.5, ...]"]
  • Done using AI embedding models (like OpenAI’s text-embedding-3-small)
  • Same meaning = close numbers

Step 2: Store & Index These Vectors

  • Not stored as plain text
  • Stored as arrays of numbers
  • Special indexes (like HNSW, IVF) make “find similar” super fast

Step 3: Search by Meaning

flowchart LR
    Q["User searches:<br/>'outdoor furniture'"] --> QV["Query Vector:<br/>[0.3, 0.6, 0.2, 0.8, ...]"]
    QV --> SIM[Cosine Similarity Search]
    SIM --> R["'cozy patio seating'<br/>'garden lounge set'"]

Vector DB vs SQL vs NoSQL: Key Differences

FeatureSQL (e.g. PostgreSQL)NoSQL (e.g. MongoDB)Vector DB
StoresRows with columnsJSON documentsVectors (numbers)
SearchExact match, filtersText search, regexSimilarity (meaning)
Best forTransactions, reportsFlexible dataAI search, recommendations
Speed at scaleSlow for similarityNot built for itBlazing fast similarity

Think of it like this:
SQL = phone book (exact name lookup)
Vector DB = friend who “knows someone like that”


Real-World Use Cases (You’re Already Using These!)

  • Smart product search
    → Finds “cozy patio” when user types “comfy outdoor”
  • Chatbots & support
    → Matches “How do I reset?” to “password recovery guide”
  • Recommendation engines
    → “Users who liked X also liked Y” (based on behavior vectors)
  • Document search
    → Finds relevant policies even if keywords differ
  • Image/audio search
    → Find similar images or songs by content

How Data Gets Added (Behind the Scenes)

(From the diagram in the tweet)

  1. User sends object → e.g., movie: {title: "Top Gun", genre: "action"}
  2. System generates vector → using AI model (e.g., OpenAI)
  3. Vector + metadata stored → in collection + indexes
  4. Inverted index updated → for fast filtering (e.g., genre = action)
  5. Vector index updated → for similarity search
  6. Object ID returned → UUID like a1b2c3...

All this happens in parallel — super fast!

flowchart TD
    O["User sends object<br/>e.g., {title: 'Top Gun', genre: 'action'}"] --> E["System generates vector<br/>(via AI model)"]
    E --> S["Vector + metadata stored<br/>(collection + indexes)"]
    S --> II["Inverted index updated<br/>(fast filtering, e.g. genre = action)"]
    S --> VI[" Vector index updated<br/>(similarity search)"]
    II --> ID["Object ID returned<br/>(UUID)"]
    VI --> ID

Popular Vector Databases (Pick Your Flavor)

NameTypeBest For
WeaviateOpen-sourceFeature-rich, self-hosted
PineconeManaged (cloud)Easy, no ops, pricey
MilvusOpen-sourceMassive scale, complex
QdrantOpen-source (Rust)Fast, lightweight
pgvectorPostgres extensionSimple, use your existing DB

Hot take: For more than 1 million items, just use PostgreSQL + pgvector.
No need for a fancy vector DB yet.


Do You Really Need a Vector Database?

Project SizeRecommendation
Less than 100K itemsUse Postgres + pgvector
100K – 1MStill fine with pgvector
More than 1M or heavy searchConsider Weaviate/Pinecone

Start simple. Scale when you feel the pain.

flowchart TD
    Q{Dataset Size?}
    Q -->|"< 100K items"| PG["Use Postgres + pgvector"]
    Q -->|"100K – 1M"| PG2["Still fine with pgvector"]
    Q -->|"> 1M or heavy search"| VDB["Consider Weaviate / Pinecone"]

TL;DR: Vector Databases in 5 Bullets

  • Turn text → meaningful numbers (embeddings)
  • Store & search by similarity, not keywords
  • Perfect for AI search, recommendations, chatbots
  • Different from SQL/NoSQL: math-based, not rule-based
  • Start with pgvector — you probably don’t need more

Want to try it today?

-- In PostgreSQL with pgvector
CREATE EXTENSION vector;
CREATE TABLE products (id serial, description text, embedding vector(1536));

-- Insert
INSERT INTO products (description, embedding)
VALUES ('cozy patio seating', '[0.3,0.8,...]');

-- Search
SELECT * FROM products
ORDER BY embedding <=> '[0.2,0.7,...]'  -- your query vector
LIMIT 5;

That’s it. You’re now doing AI-powered search.


MongoDB Also Does Vector Search! (Atlas Vector Search)

Yes! MongoDB added native vector search in MongoDB Atlas (cloud version).

Why Use MongoDB for Vectors?

  • You already use MongoDB? → No new database
  • Store documents + vectors together
  • Full-text + vector search in one query
  • Great for apps with rich metadata

MongoDB Vector Search – Basic Implementation (Python using pymongo)

# MongoDB Vector Search –

from pymongo import MongoClient

# 1. Connect to MongoDB Atlas (replace <connection_string>)
client = MongoClient("mongodb+srv://<user>:<password>@cluster0.mongodb.net/")
db = client["store"]
collection = db["products"]

# 2. Insert a document with embedding
collection.insert_one({
    "description": "cozy patio seating",
    "price": 299,
    "category": "outdoor",
    "embedding": [0.3, 0.8, 0.1, ..., 0.9]  # 1536-dim vector (example)
})

# 3. Search using vector similarity
pipeline = [
    {
        "$vectorSearch": {
            "index": "vector_index",  # Name of your vector search index
            "path": "embedding",
            "queryVector": [0.2, 0.7, 0.1, ..., 0.4],  # Embedding of user query
            "numCandidates": 100,
            "limit": 5
        }
    },
    {
        "$project": {
            "description": 1,
            "price": 1,
            "category": 1,
            "score": { "$meta": "vectorSearchScore" }
        }
    }
]

results = list(collection.aggregate(pipeline))

# Print results
for doc in results:
    print(f"{doc['description']} | Score: {doc['score']:.4f}")

Note:

  • You must create a Vector Search Index in MongoDB Atlas first (via UI or CLI)
  • Use pip install pymongo to install the driver
  • Replace the placeholder vector with real 1536-dim embeddings (e.g., from OpenAI)

pgvector vs MongoDB Atlas Vector Search: Quick Comparison

FeaturePostgreSQL + pgvectorMongoDB Atlas Vector Search
Open sourceYesNo (cloud only)
Self-hostableYesNo
Works with existing dataYesYes
Full-text + vector in one queryYes (with tsvector)Yes
Free tierYesYes (limited)
Best forSmall–medium apps, full controlApps already on MongoDB, rapid prototyping

Rule of thumb:
Use pgvector if you want free, open, self-hosted
Use MongoDB Atlas if you’re already in the MongoDB ecosystem


Cheers,

Sim