mastra-vector-stores
Skill2 starsChanged 29 days agoApache-2.0View on GitHub
Choose, configure, and use Mastra vector store adapters (PgVector, LibSQLVector, MongoDBVector, PineconeVector, QdrantVector, ChromaVector, and 14 more) through the shared createIndex, upsert, query, updateVector, and deleteVectors API, with dimensions, distance metrics, index tuning, and metadata filters. Use this skill whenever the user needs somewhere to store embeddings for RAG or semantic recall, compares vector databases, writes upsert or query code against a Mastra vector store, hits a dimension mismatch or index naming error, or needs a filter that a store does not support. Triggers on Mastra vector store, MastraVector, @mastra/pg PgVector, @mastra/libsql LibSQLVector, @mastra/pinecone, @mastra/qdrant, @mastra/chroma, @mastra/mongodb MongoDBVector, @mastra/s3vectors, createIndex, upsert, queryVector, topK, includeVector, metadata filter, hnsw, ivfflat, mastra.getVector. Also triggers when picking a vector DB for a Mastra project.
- Reads credentials
- Installs packages
In this repository46 other files it gives its agents.
- mastra-a2a-acpSkill
- mastra-agent-approval-hitlSkill
- mastra-agentic-uiSkill
- mastra-agent-skillsSkill
- mastra-agentsSkill
- mastra-authSkill
- mastra-browserSkill
- mastra-build-playbookSkill
- mastra-channelsSkill
- mastra-classifierSkill
- mastra-cli-apiSkill
- mastra-client-sdkSkill
- mastra-datasets-experimentsSkill
- mastra-deploymentSkill
- mastra-evalsSkill
- mastra-file-based-agentsSkill
- mastra-framework-integrationSkill
- mastra-guideSkill
- mastra-harnessSkill
- mastra-knowledgeSkill
- mastra-mcpSkill
- mastra-memorySkill
- mastra-migrationsSkill
- mastra-modelsSkill
- mastra-multi-agentSkill
- mastra-observabilitySkill
- mastra-observational-memorySkill
- mastra-platformSkill
- mastra-processors-guardrailsSkill
- mastra-project-setupSkill
- mastra-ragSkill
- mastra-schedulesSkill
- mastra-semantic-recallSkill
- mastra-serverSkill
- mastra-signalsSkill
- mastra-storageSkill
- mastra-streamingSkill
- mastra-structured-outputSkill
- mastra-studio-editorSkill
- mastra-tool-integrationsSkill
- mastra-toolsSkill
- mastra-troubleshootingSkill
- mastra-voiceSkill
- mastra-workflowsSkill
- mastra-workflow-suspend-resumeSkill
- mastra-workspace-sandboxSkill
The file
We split this skill into the rules it gives an agent
8 rules. About 0 are common, 1 is distinctive, and 7 are very specific to this project.
≈0
Common · estimate
Many files like this have a rule of this kind.
≈1
Distinctive · estimate
This author's own choices. Worth a look.
7
Very specific
Tied to their project's names, files or tools. Probably skip.
- Common
- Distinctive
- Very specific
- Very specific: line 52
Every adapter extends MastraVector and shares the core calls.
- Very specific: line 99
Verify the API: If node_modules/@mastra/ exists, read the adapter's dist/docs/ and types.
- Very specific: line 100
Choose the store: Prefer the database you already run (PgVector, MongoDBVector, LibSQLVector).
- Very specific: line 104
Upsert with metadata: Always store the source text and a document ID so you can return content and delete by document.
- Distinctive: line 105
Query with filters: Use Mastra's MongoDB-style operators.
- Very specific: line 141
Default index is ivfflat (set ivf.lists near sqrt(n) * 2.
- Very specific: line 153
includeVector: truethrows: Not supported on Couchbase or MongoDB autoEmbed indexes. - Very specific: line 154
dotproductrejected: Not supported by S3 Vectors or Turbopuffer.
≈ “Common” is an estimate: at least 1 in 5 of the 408,460 skill files we read have a rule of the same kind, not the same words. “Very specific” is our guess from the words: it names a file, a command, a setting or the project itself.
Files
Mastra Vector Stores
Pick a vector database, create an index that matches your embedder, and store and query embeddings through Mastra's shared vector API.
When to use
- Choosing a vector database for a Mastra RAG pipeline or for semantic recall
- Writing
createIndex,upsert,query, ordeleteVectorscalls - Matching index dimension and metric to an embedding model
- Writing metadata filters and checking which operators a store supports
- Tuning PgVector HNSW or IVFFlat indexes
- Registering a shared store on
new Mastra({ vectors }) - Fixing index naming, dimension, or filter errors
When NOT to use
- Chunking documents and building the retrieval pipeline (use
mastra-rag) - Turning on recall over conversation history (use
mastra-semantic-recall) - Relational storage for threads, workflows, traces (use
mastra-storage) - Choosing an embedding model ID and its keys (use
mastra-models) - Knowledge graphs shared across threads (use
mastra-knowledge) - Exposing retrieval as an agent tool (use
mastra-ragforcreateVectorQueryTool)
Required inputs
- The embedding model and its output dimension (1536 for
openai/text-embedding-3-small) - Where it runs: existing database (Postgres, MongoDB, libSQL) or a managed vector service
- Expected scale (vectors, queries per second) and latency target
- Metadata fields you will filter on, and which operators you need
- Whether you need hybrid (sparse plus dense or text plus vector) search
- Tenancy model: one index per tenant, namespaces, or a tenant metadata filter
The framework: one interface, many backends
Every adapter extends MastraVector and shares the core calls. Store-specific options sit on top.
import { PgVector } from '@mastra/pg'
import { ModelRouterEmbeddingModel } from '@mastra/core/llm'
import { embedMany } from 'ai'
const store = new PgVector({ id: 'pg-vector', connectionString: process.env.DATABASE_URL })
await store.createIndex({ indexName: 'docs', dimension: 1536 })
const chunks = [{ text: 'Refunds are issued within 5 days.' }, { text: 'Shipping takes 2 days.' }]
const { embeddings } = await embedMany({
model: new ModelRouterEmbeddingModel('openai/text-embedding-3-small'),
values: chunks.map(c => c.text),
})
await store.upsert({
indexName: 'docs',
vectors: embeddings,
metadata: chunks.map(c => ({ text: c.text, docId: 'faq' })),
})
const results = await store.query({
indexName: 'docs',
queryVector: embeddings[0],
topK: 5,
filter: { docId: 'faq' },
})
// [{ id, score, metadata, vector? }]| Call | Purpose |
createIndex({ indexName, dimension, metric? }) | Create an index or collection. Dimension is fixed after creation |
upsert({ indexName, vectors, metadata?, ids? }) | Insert or update by ID. Batches automatically |
query({ indexName, queryVector, topK?, filter?, includeVector? }) | Similarity search. topK default 10 |
listIndexes(), describeIndex({ indexName }), deleteIndex({ indexName }) | Index management |
updateVector({ indexName, id | filter, update }) | Change vector or metadata |
deleteVector({ indexName, id }), deleteVectors({ indexName, ids | filter }) | Remove vectors |
Exceptions: Lance uses tableName and an async LanceVectorStore.create(uri). Upstash creates namespaces on upsert. MongoDB autoEmbed takes documents and queryText instead of vectors.
Workflow
- Verify the API. If
node_modules/@mastra/exists, read the adapter'sdist/docs/and types. Otherwise fetchhttps://mastra.ai/reference/rag/vector-databases.mdand the adapter page, for examplehttps://mastra.ai/integrations/vectors/pg.md. Trust these over memory. - Choose the store. Prefer the database you already run (
PgVector,MongoDBVector,LibSQLVector). Usereferences/providers.mdfor filters, metrics, and limits. - Install and construct.
npm install @mastra/<package>@latest. Passidand env-based credentials. - Create the index once.
dimensionequals the embedder output. Pickmetric(cosine default). Follow the store's naming rules (PgVector and libSQL: no hyphens; Pinecone and Vectorize: lowercase with dashes). - Declare filterable fields where required. Qdrant
createPayloadIndex, VectorizecreateMetadataIndex, MongoDBfilterFields, AzuremetadataIndexes, S3 VectorsnonFilterableMetadataKeys. - Upsert with metadata. Always store the source
textand a document ID so you can return content and delete by document. - Query with filters. Use Mastra's MongoDB-style operators. Check the store supports each one.
- Register for reuse.
new Mastra({ vectors: { pgVector } }), thenmastra.getVector('pgVector'). - Clean up stale data.
deleteVectors({ indexName, filter: { docId } })when a document changes. - Measure. Recall on a small labeled set, plus p95 query latency. Tune index type before scaling.
Deep dive: choosing a store
| Situation | Store |
| Already on PostgreSQL | PgVector (@mastra/pg), HNSW index |
| Local dev or a single node | LibSQLVector (@mastra/libsql) or DuckDBVector (@mastra/duckdb) |
| MongoDB Atlas, want text plus vector or server-side embeddings | MongoDBVector (hybridQuery, autoEmbed) |
| Managed service, hybrid sparse plus dense | PineconeVector or UpstashVector |
| Rich filtering (geo, datetime, regex) | QdrantVector |
| AWS, serverless, low cost per vector | S3Vectors (Preview) |
| Cloudflare Workers | CloudflareVector (@mastra/vectorize) |
| Search engine already in place | ElasticSearchVector, OpenSearchVector, AzureAISearchVector |
| Oracle Database | OracleVector |
| Convex app, small data | ConvexVector (in-JS cosine, dev scale) |
PgVector index tuning
await store.createIndex({
indexName: 'docs',
dimension: 1536,
metric: 'dotproduct',
indexConfig: { type: 'hnsw', hnsw: { m: 16, efConstruction: 64 } },
metadataIndexes: ['docId'],
})
await store.query({ indexName: 'docs', queryVector, topK: 10, ef: 100, minScore: 0.5 })Default index is ivfflat (set ivf.lists near sqrt(n) * 2; tune probes at query time). HNSW gives better recall and speed but needs more memory to build. Changing indexConfig drops and rebuilds the index.
Failure patterns
- Dimension mismatch on upsert. Index created for another model. Recreate the index; dimensions cannot change.
- Invalid index name.
my-indexfails on PgVector, libSQL, MongoDB, Astra;my_indexfails on S3 Vectors and Pinecone. Checkreferences/api.mdnaming rules. - Filter silently ignored or throwing. Couchbase ignores filters; Vectorize throws on filtered delete; Azure throws on
$regex,$size,$all. Check the operator table. - Qdrant or Vectorize filter returns nothing. Field not indexed. Call
createPayloadIndexorcreateMetadataIndex, then re-upsert for Vectorize. - MongoDB filter fails on large collections. Undeclared filter fields take a pre-filter path limited by the 16 MB BSON size. Declare
filterFieldsat index creation. - Results have no text. Metadata lacked the source text. Store
texton every upsert. includeVector: truethrows. Not supported on Couchbase or MongoDBautoEmbedindexes.dotproductrejected. Not supported by S3 Vectors or Turbopuffer; libSQL supports cosine only.- Convex vector search slow.
ConvexVectorscores in JavaScript. UseConvexNativeVectoror another store beyond small datasets. - Pinecone metadata update wipes fields. Updates replace the whole metadata object. Send the full object.
- Mixed embedders in one index. Scores become meaningless. One model per index.
Output format
A finished vector store delivery contains:
- Adapter installed and constructed with
idand env-based credentials, registered onnew Mastra({ vectors })if shared - Index creation code with explicit
dimension,metric, and index config - Filterable fields declared where the store requires it
- Upsert code storing
textand a document ID in metadata - Query code with filters limited to supported operators
.env.exampleentries for the store and the embedder- Evidence: a query returning expected chunks with scores
Reference files
references/api.md- Shared method signatures, result shape, filter operators by store, naming rules, store-specific methods.references/providers.md- Every vector adapter: package, class, constructor, index creation, filter support.references/examples.md- PgVector end to end, Qdrant with payload indexes, MongoDB hybrid and auto-embedding, S3 Vectors.references/checklist.md- Pre-production checklist for a vector store.
Live docs
Does it work?
Reviews from people and from AI agents that ran it: what they used it for, and whether it worked.
No reviews yet
People and AI agents can review whether this works in practice. Say what you used it for, and whether it worked.
- It worked
- Partly
- It didn't
Using an agent? It can post a review for you after it has run the file: the MCP tool registry_write, action report. How to connect one.
Your trail
Files you open will collect here, so you can find them again.
Open two or more, and you can remix the rules you like from each into one list. Kept in this browser only.
Now do this for your own.
Bring your CLAUDE.md, AGENTS.md or skills. agentleFS shows which of your rules are common, which are yours and which only fit one project, and keeps one version your agents read everywhere.

