mongodb-ai
npx machina-cli add skill romiluz13/mongodb-agent-skills/mongodb-ai --openclawMongoDB 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,
$vectorSearchstage, 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
$vectorSearchaggregation queries - Tuning numCandidates for recall vs. latency
- Implementing RAG (Retrieval-Augmented Generation)
- Building hybrid search with
$rankFusionor$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-4orvoyage-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
| Priority | Category | Impact | Prefix | Rules |
|---|---|---|---|---|
| 1 | Vector Index Creation | CRITICAL | index- | 9 |
| 2 | $vectorSearch Queries | CRITICAL | query- | 7 |
| 3 | Performance Tuning | HIGH | perf- | 6 |
| 4 | RAG Patterns | HIGH | rag- | 4 |
| 5 | Hybrid Search | MEDIUM | hybrid- | 4 |
| 6 | AI Agent Integration | MEDIUM | agent- | 3 |
Quick Reference
1. Vector Index Creation (CRITICAL) - 9 rules
index-vector-definition- Required fields: type, path, numDimensions, similarityindex-similarity-function- Choosing cosine vs euclidean vs dotProductindex-filter-fields- Pre-filtering with filter type indexesindex-quantization- Scalar (3.75x) vs binary (24x) RAM reductionindex-dimensions-match- numDimensions must match embedding modelindex-multitenant- Single collection with tenant_id for SaaS appsindex-views-partial- Partial indexing via MongoDB Viewsindex-hnsw-options- maxEdges/numEdgeCandidates tuningindex-automated-embedding- Server-side embedding with Voyage AI
2. $vectorSearch Queries (CRITICAL) - 7 rules
query-vectorsearch-first- MUST be first stage in aggregation pipelinequery-numcandidates-tuning- The 20x rule for recall vs latencyquery-ann-vs-enn- When to use exact: truequery-prefiltering- Filter before vector comparison ($exists, $ne, $not)query-lexical-prefilter- Advanced text filters (fuzzy, phrase, geo) via $search.vectorSearchquery-get-scores- Using $meta: "vectorSearchScore"query-same-embedding-model- Data/query embeddings must share space, dimensions, and correctinput_type
3. Performance Tuning (HIGH) - 6 rules
perf-quantization-scale- Enable at 100K+ vectorsperf-index-in-memory- Vector indexes must fit in RAMperf-numcandidates-tradeoff- Benchmark recall/latency/cost trade-offs by model andnumCandidatesperf-prefilter-narrow- Reduce candidate set before vector comparisonperf-explain-vectorsearch- Debug with explain() for vector queriesperf-search-nodes- Dedicated Search Nodes for production
4. RAG Patterns (HIGH) - 4 rules
rag-ingestion-pattern- Store documents with embeddingsrag-retrieval-pattern- $vectorSearch for context retrievalrag-context-window- Managing LLM context limitsrag-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 tuninghybrid-limitations- Stage restrictions plus decision matrix ($rankFusionvs$scoreFusionvs retrieval+rerank)
6. AI Agent Integration (MEDIUM) - 3 rules
agent-memory-schema- Short-term vs long-term memory designagent-memory-retrieval- Semantic search over memoriesagent-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)
| limit | numCandidates | Max allowed |
|---|---|---|
| 10 | 200 | 10,000 |
| 50 | 1,000 | 10,000 |
| 100 | 2,000 | 10,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)
| Need | Canonical Doc |
|---|---|
Vector index definition (vector / autoEmbed) | Atlas Vector Search Type |
| Query stage syntax and operator support | Atlas $vectorSearch Stage |
| Hybrid overview and limitations | Atlas Hybrid Search |
$vectorSearch in $rankFusion input | Vector Search with $rankFusion |
| Fusion stage availability | MongoDB $rankFusion and MongoDB $scoreFusion |
Voyage model behavior and input_type | Voyage Quickstart and Voyage Text Embeddings |
| Voyage reranker model docs | Voyage Rerankers |
| Latest feature/release shifts | Atlas 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 Type | MCP Tools | Action |
|---|---|---|
| Read (Safe) | find, aggregate, explain, collection-indexes | May run automatically to verify |
| Write (Requires Approval) | create-index, insert-many | Show 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:
- Different embedding model for data vs query
- Index still building
- 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
- Step 1: Create a vector index on your collection specifying type, path, numDimensions, and similarity
- Step 2: Write a $vectorSearch stage in an aggregation, with optional pre-filtering and lexical filters
- 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