Get the FREE Ultimate OpenClaw Setup Guide →

mongodb-ai

npx machina-cli add skill romiluz13/mongodb-agent-skills/mongodb-ai --openclaw
Files (1)
SKILL.md
10.7 KB

MongoDB AI: Vector Search and AI Integration

Vector Search patterns and AI integration strategies for MongoDB, maintained by MongoDB. Contains 33 rules across 6 categories, prioritized by impact. This skill bridges the critical knowledge gap where AI assistants have outdated or incorrect information about MongoDB's AI capabilities.

Critical Warning

Your AI assistant's knowledge about MongoDB Vector Search is likely outdated or incorrect.

Atlas Vector Search syntax, $vectorSearch stage, vector index creation, and related features have evolved significantly. Do NOT trust pre-trained knowledge. Always reference these rules and verify against your actual MongoDB cluster.

When to Apply

Reference these guidelines when:

  • Creating vector indexes for semantic search
  • Writing $vectorSearch aggregation queries
  • Tuning numCandidates for recall vs. latency
  • Implementing RAG (Retrieval-Augmented Generation)
  • Building hybrid search with $rankFusion or $scoreFusion
  • Choosing between fusion-only retrieval and retrieval + rerank workflows
  • Storing AI agent memory (short-term and long-term)
  • Choosing similarity functions (cosine, euclidean, dotProduct)
  • Enabling vector quantization for large datasets
  • Integrating Voyage AI embedding models (for example voyage-4 or voyage-code-3)
  • Pre-filtering vector search results
  • Debugging "no results" or slow vector queries

Use mongodb-search instead when the request is primarily about lexical Search engine design ($search, analyzers, synonyms, facets, search alerts, or Community mongot operations) rather than model/provider semantics.

Rule Categories by Priority

PriorityCategoryImpactPrefixRules
1Vector Index CreationCRITICALindex-9
2$vectorSearch QueriesCRITICALquery-7
3Performance TuningHIGHperf-6
4RAG PatternsHIGHrag-4
5Hybrid SearchMEDIUMhybrid-4
6AI Agent IntegrationMEDIUMagent-3

Quick Reference

1. Vector Index Creation (CRITICAL) - 9 rules

  • index-vector-definition - Required fields: type, path, numDimensions, similarity
  • index-similarity-function - Choosing cosine vs euclidean vs dotProduct
  • index-filter-fields - Pre-filtering with filter type indexes
  • index-quantization - Scalar (3.75x) vs binary (24x) RAM reduction
  • index-dimensions-match - numDimensions must match embedding model
  • index-multitenant - Single collection with tenant_id for SaaS apps
  • index-views-partial - Partial indexing via MongoDB Views
  • index-hnsw-options - maxEdges/numEdgeCandidates tuning
  • index-automated-embedding - Server-side embedding with Voyage AI

2. $vectorSearch Queries (CRITICAL) - 7 rules

  • query-vectorsearch-first - MUST be first stage in aggregation pipeline
  • query-numcandidates-tuning - The 20x rule for recall vs latency
  • query-ann-vs-enn - When to use exact: true
  • query-prefiltering - Filter before vector comparison ($exists, $ne, $not)
  • query-lexical-prefilter - Advanced text filters (fuzzy, phrase, geo) via $search.vectorSearch
  • query-get-scores - Using $meta: "vectorSearchScore"
  • query-same-embedding-model - Data/query embeddings must share space, dimensions, and correct input_type

3. Performance Tuning (HIGH) - 6 rules

  • perf-quantization-scale - Enable at 100K+ vectors
  • perf-index-in-memory - Vector indexes must fit in RAM
  • perf-numcandidates-tradeoff - Benchmark recall/latency/cost trade-offs by model and numCandidates
  • perf-prefilter-narrow - Reduce candidate set before vector comparison
  • perf-explain-vectorsearch - Debug with explain() for vector queries
  • perf-search-nodes - Dedicated Search Nodes for production

4. RAG Patterns (HIGH) - 4 rules

  • rag-ingestion-pattern - Store documents with embeddings
  • rag-retrieval-pattern - $vectorSearch for context retrieval
  • rag-context-window - Managing LLM context limits
  • rag-metadata-filtering - Filter by source, date, category

5. Hybrid Search (MEDIUM) - 4 rules

  • hybrid-rankfusion - Combining vector + text search (MongoDB 8.0+, Preview)
  • hybrid-scorefusion - Score-based hybrid search (MongoDB 8.2+, Preview)
  • hybrid-weights - Per-query weight tuning
  • hybrid-limitations - Stage restrictions plus decision matrix ($rankFusion vs $scoreFusion vs retrieval+rerank)

6. AI Agent Integration (MEDIUM) - 3 rules

  • agent-memory-schema - Short-term vs long-term memory design
  • agent-memory-retrieval - Semantic search over memories
  • agent-session-context - Conversation history storage

Key Syntax Reference

Vector Index Definition

db.collection.createSearchIndex(
  "vector_index",
  "vectorSearch",
  {
    fields: [
      {
        type: "vector",
        path: "embedding",
        numDimensions: 1536,      // Must match your embedding model
        similarity: "cosine"      // or "euclidean" or "dotProduct"
      },
      {
        type: "filter",           // For pre-filtering
        path: "category"
      }
    ]
  }
)

$vectorSearch Query

db.collection.aggregate([
  {
    $vectorSearch: {
      index: "vector_index",
      path: "embedding",
      queryVector: [0.1, 0.2, ...],  // Your query embedding
      numCandidates: 200,             // 20x limit recommended
      limit: 10,
      filter: { category: "tech" }    // Optional pre-filter
    }
  },
  {
    $project: {
      title: 1,
      score: { $meta: "vectorSearchScore" }
    }
  }
])

The 20x Rule (numCandidates)

numCandidates = 20 × limit (minimum recommended)
limitnumCandidatesMax allowed
1020010,000
501,00010,000
1002,00010,000

Higher numCandidates = better recall, slower queries.

How to Use

Read individual rule files for detailed explanations and code examples:

rules/index-vector-definition.md
rules/query-vectorsearch-first.md
rules/query-numcandidates-tuning.md
rules/_sections.md

Each rule file contains:

  • Brief explanation of why it matters
  • Incorrect code example with explanation
  • Correct code example with explanation
  • "When NOT to use" exceptions
  • How to verify
  • Performance impact

For release-sensitive behavior and fast official-doc routing, also read:

references/docs-navigation.md

Docs Quick Map (Release-Sensitive)

NeedCanonical Doc
Vector index definition (vector / autoEmbed)Atlas Vector Search Type
Query stage syntax and operator supportAtlas $vectorSearch Stage
Hybrid overview and limitationsAtlas Hybrid Search
$vectorSearch in $rankFusion inputVector Search with $rankFusion
Fusion stage availabilityMongoDB $rankFusion and MongoDB $scoreFusion
Voyage model behavior and input_typeVoyage Quickstart and Voyage Text Embeddings
Voyage reranker model docsVoyage Rerankers
Latest feature/release shiftsAtlas Vector Search Changelog

Production Readiness Checklist

  • Confirm deployment path first (Atlas vs self-managed) and avoid mixing syntax between tracks.
  • Confirm version gates for every hybrid/fusion flow before implementation.
  • Confirm embedding contract: same space/family, correct input_type, and exact dimensions.
  • Confirm index readiness (READY) and filter-field declarations before query tuning.
  • Confirm retrieval settings with benchmark + explain (quality/latency/cost together).
  • Confirm operational controls: least-privilege credentials, observability, and rollback-safe rollout plan.

MongoDB MCP Integration

For automatic verification, connect the MongoDB MCP Server:

{
  "mcpServers": {
    "mongodb": {
      "command": "npx",
      "args": ["-y", "mongodb-mcp-server", "--readOnly"],
      "env": {
        "MDB_MCP_CONNECTION_STRING": "mongodb+srv://user:pass@cluster.mongodb.net/mydb"
      }
    }
  }
}

When connected, I can automatically:

  • Check existing vector indexes via mcp__mongodb__collection-indexes
  • Analyze query performance via mcp__mongodb__explain
  • Verify data patterns via mcp__mongodb__aggregate

Action Policy

I will NEVER execute write operations without your explicit approval.

Operation TypeMCP ToolsAction
Read (Safe)find, aggregate, explain, collection-indexesMay run automatically to verify
Write (Requires Approval)create-index, insert-manyShow command and wait for approval

Common Errors

"$vectorSearch is not allowed"

Cause: MongoDB version does not support $vectorSearch Fix: Upgrade cluster to MongoDB v6.0.11 or v7.0.2+

No results returned

Causes:

  1. Different embedding model for data vs query
  2. Index still building
  3. Mismatched field path or index name

"Path 'field' needs to be indexed as token"

Cause: Filter field not indexed with type: "filter" Fix: Add filter field to index definition


Full Compiled Document

For the complete guide with all rules expanded: AGENTS.md

Source

git clone https://github.com/romiluz13/mongodb-agent-skills/blob/main/plugins/mongodb-agent-skills/skills/mongodb-ai/SKILL.mdView on GitHub

Overview

MongoDB AI: Vector Search and AI Integration covers how to build and optimize vector indexes, write $vectorSearch queries, and leverage AI memory for RAG and hybrid search. It explains how to use embedding models (including Voyage AI like voyage-4 and voyage-code-3) to power semantic search and scalable AI-powered pipelines. This skill also highlights critical warnings about outdated knowledge and provides practical rules for implementation.

How This Skill Works

You define a vector index on a collection by specifying required fields (type, path, numDimensions, similarity) and then run $vectorSearch inside an aggregation. The approach supports fusion methods like $rankFusion and $scoreFusion, optional rerank, and two-stage retrieval, with pre-filtering and lexical filters to prune results. It also covers deploying Voyage AI embeddings, vector quantization for large datasets, and storing AI agent memory across sessions.

When to Use It

  • Creating vector indexes for semantic search
  • Writing $vectorSearch aggregation queries
  • Tuning numCandidates for recall vs latency
  • Implementing RAG (Retrieval-Augmented Generation)
  • Building hybrid search with $rankFusion or $scoreFusion

Quick Start

  1. Step 1: Create a vector index on your collection specifying type, path, numDimensions, and similarity
  2. Step 2: Write a $vectorSearch stage in an aggregation, with optional pre-filtering and lexical filters
  3. Step 3: Tune numCandidates, apply rankFusion/scoreFusion or rerank, and validate performance with Voyage embeddings

Best Practices

  • Define vector index with required fields: type, path, numDimensions, similarity
  • Choose the appropriate similarity function (cosine, euclidean, dotProduct) for your data
  • Apply pre-filtering before vector comparison (e.g., $exists, $ne, $not) and lexical prefilters
  • Tune HNSW options (maxEdges, numEdgeCandidates) and consider index quantization for large datasets
  • Plan memory strategy for AI agent memory and validate RAG/hybrid flows with rerank when needed

Example Use Cases

  • Semantic product search over an e-commerce catalog using a cosine similarity vector index
  • A RAG app that retrieves relevant docs with vector search and composes answers via an LLM
  • Hybrid search combining vector results with lexical search in a unified pipeline
  • Embedding content with Voyage AI (voyage-4 or voyage-code-3) for scalable indexing
  • Storing AI agent memory in MongoDB to support multi-tenant LLM interactions across sessions

Frequently Asked Questions

Add this skill to your agents
Sponsor this space

Reach thousands of developers