@mastra/memory 0.0.0-error-handler-fix-20251020202607 → 0.0.0-esbuild-bundle-worker-20260807182016
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +4656 -3
- package/LICENSE.md +15 -0
- package/README.md +26 -1
- package/dist/_types/@internal_ai-sdk-v4/dist/index.d.ts +7450 -0
- package/dist/docs/SKILL.md +62 -0
- package/dist/docs/assets/SOURCE_MAP.json +11 -0
- package/dist/docs/references/docs-agents-agent-approval.md +664 -0
- package/dist/docs/references/docs-agents-networks.md +184 -0
- package/dist/docs/references/docs-capabilities-subagents.md +454 -0
- package/dist/docs/references/docs-evals-evals-with-memory.md +146 -0
- package/dist/docs/references/docs-long-running-agents-background-tasks.md +382 -0
- package/dist/docs/references/docs-long-running-agents-goals.md +118 -0
- package/dist/docs/references/docs-memory-memory-processors.md +385 -0
- package/dist/docs/references/docs-memory-message-history.md +348 -0
- package/dist/docs/references/docs-memory-multi-user-threads.md +208 -0
- package/dist/docs/references/docs-memory-observational-memory.md +835 -0
- package/dist/docs/references/docs-memory-overview.md +266 -0
- package/dist/docs/references/docs-memory-semantic-recall.md +401 -0
- package/dist/docs/references/docs-memory-working-memory.md +431 -0
- package/dist/docs/references/docs-storage-overview.md +214 -0
- package/dist/docs/references/reference-core-getMemory.md +51 -0
- package/dist/docs/references/reference-core-listMemory.md +57 -0
- package/dist/docs/references/reference-file-based-agents-memory.md +58 -0
- package/dist/docs/references/reference-memory-clone-utilities.md +203 -0
- package/dist/docs/references/reference-memory-cloneThread.md +173 -0
- package/dist/docs/references/reference-memory-createThread.md +70 -0
- package/dist/docs/references/reference-memory-getThreadById.md +26 -0
- package/dist/docs/references/reference-memory-listThreads.md +147 -0
- package/dist/docs/references/reference-memory-memory-class.md +148 -0
- package/dist/docs/references/reference-memory-observational-memory.md +877 -0
- package/dist/docs/references/reference-memory-summarizeConversation.md +99 -0
- package/dist/docs/references/reference-memory-summarizeThread.md +93 -0
- package/dist/docs/references/reference-processors-token-limiter-processor.md +158 -0
- package/dist/docs/references/reference-storage-dsql.md +430 -0
- package/dist/docs/references/reference-storage-dynamodb.md +284 -0
- package/dist/docs/references/reference-storage-libsql.md +143 -0
- package/dist/docs/references/reference-storage-mongodb.md +267 -0
- package/dist/docs/references/reference-storage-postgresql.md +531 -0
- package/dist/docs/references/reference-storage-redis.md +268 -0
- package/dist/docs/references/reference-storage-upstash.md +162 -0
- package/dist/docs/references/reference-vectors-libsql.md +307 -0
- package/dist/docs/references/reference-vectors-mongodb.md +567 -0
- package/dist/docs/references/reference-vectors-pg.md +430 -0
- package/dist/docs/references/reference-vectors-upstash.md +296 -0
- package/dist/index.cjs +35 -893
- package/dist/index.d.ts +465 -62
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +2 -887
- package/dist/processors/index.cjs +32 -165
- package/dist/processors/index.d.ts +1 -2
- package/dist/processors/index.d.ts.map +1 -1
- package/dist/processors/index.js +2 -158
- package/dist/processors/observational-memory/activation-ttl.d.ts +4 -0
- package/dist/processors/observational-memory/activation-ttl.d.ts.map +1 -0
- package/dist/processors/observational-memory/anchor-ids.d.ts +4 -0
- package/dist/processors/observational-memory/anchor-ids.d.ts.map +1 -0
- package/dist/processors/observational-memory/buffering-coordinator.d.ts +61 -0
- package/dist/processors/observational-memory/buffering-coordinator.d.ts.map +1 -0
- package/dist/processors/observational-memory/built-in-extractors.d.ts +15 -0
- package/dist/processors/observational-memory/built-in-extractors.d.ts.map +1 -0
- package/dist/processors/observational-memory/constants.d.ts +74 -0
- package/dist/processors/observational-memory/constants.d.ts.map +1 -0
- package/dist/processors/observational-memory/date-utils.d.ts +41 -0
- package/dist/processors/observational-memory/date-utils.d.ts.map +1 -0
- package/dist/processors/observational-memory/debug.d.ts +3 -0
- package/dist/processors/observational-memory/debug.d.ts.map +1 -0
- package/dist/processors/observational-memory/extracted-values.d.ts +44 -0
- package/dist/processors/observational-memory/extracted-values.d.ts.map +1 -0
- package/dist/processors/observational-memory/extraction-runner.d.ts +22 -0
- package/dist/processors/observational-memory/extraction-runner.d.ts.map +1 -0
- package/dist/processors/observational-memory/extractor.d.ts +76 -0
- package/dist/processors/observational-memory/extractor.d.ts.map +1 -0
- package/dist/processors/observational-memory/index.d.ts +30 -0
- package/dist/processors/observational-memory/index.d.ts.map +1 -0
- package/dist/processors/observational-memory/internal-request-context.d.ts +17 -0
- package/dist/processors/observational-memory/internal-request-context.d.ts.map +1 -0
- package/dist/processors/observational-memory/markers.d.ts +118 -0
- package/dist/processors/observational-memory/markers.d.ts.map +1 -0
- package/dist/processors/observational-memory/message-utils.d.ts +83 -0
- package/dist/processors/observational-memory/message-utils.d.ts.map +1 -0
- package/dist/processors/observational-memory/model-by-input-tokens.d.ts +14 -0
- package/dist/processors/observational-memory/model-by-input-tokens.d.ts.map +1 -0
- package/dist/processors/observational-memory/model-context.d.ts +2 -0
- package/dist/processors/observational-memory/model-context.d.ts.map +1 -0
- package/dist/processors/observational-memory/observation-groups.d.ts +15 -0
- package/dist/processors/observational-memory/observation-groups.d.ts.map +1 -0
- package/dist/processors/observational-memory/observation-strategies/async-buffer.d.ts +39 -0
- package/dist/processors/observational-memory/observation-strategies/async-buffer.d.ts.map +1 -0
- package/dist/processors/observational-memory/observation-strategies/base.d.ts +122 -0
- package/dist/processors/observational-memory/observation-strategies/base.d.ts.map +1 -0
- package/dist/processors/observational-memory/observation-strategies/index.d.ts +7 -0
- package/dist/processors/observational-memory/observation-strategies/index.d.ts.map +1 -0
- package/dist/processors/observational-memory/observation-strategies/resource-scoped.d.ts +43 -0
- package/dist/processors/observational-memory/observation-strategies/resource-scoped.d.ts.map +1 -0
- package/dist/processors/observational-memory/observation-strategies/sync.d.ts +41 -0
- package/dist/processors/observational-memory/observation-strategies/sync.d.ts.map +1 -0
- package/dist/processors/observational-memory/observation-strategies/types.d.ts +103 -0
- package/dist/processors/observational-memory/observation-strategies/types.d.ts.map +1 -0
- package/dist/processors/observational-memory/observation-turn/index.d.ts +4 -0
- package/dist/processors/observational-memory/observation-turn/index.d.ts.map +1 -0
- package/dist/processors/observational-memory/observation-turn/load-memory-context.d.ts +9 -0
- package/dist/processors/observational-memory/observation-turn/load-memory-context.d.ts.map +1 -0
- package/dist/processors/observational-memory/observation-turn/step.d.ts +53 -0
- package/dist/processors/observational-memory/observation-turn/step.d.ts.map +1 -0
- package/dist/processors/observational-memory/observation-turn/turn.d.ts +139 -0
- package/dist/processors/observational-memory/observation-turn/turn.d.ts.map +1 -0
- package/dist/processors/observational-memory/observation-turn/types.d.ts +46 -0
- package/dist/processors/observational-memory/observation-turn/types.d.ts.map +1 -0
- package/dist/processors/observational-memory/observation-utils.d.ts +16 -0
- package/dist/processors/observational-memory/observation-utils.d.ts.map +1 -0
- package/dist/processors/observational-memory/observational-memory.d.ts +930 -0
- package/dist/processors/observational-memory/observational-memory.d.ts.map +1 -0
- package/dist/processors/observational-memory/observer-agent.d.ts +190 -0
- package/dist/processors/observational-memory/observer-agent.d.ts.map +1 -0
- package/dist/processors/observational-memory/observer-runner.d.ts +141 -0
- package/dist/processors/observational-memory/observer-runner.d.ts.map +1 -0
- package/dist/processors/observational-memory/operation-registry.d.ts +14 -0
- package/dist/processors/observational-memory/operation-registry.d.ts.map +1 -0
- package/dist/processors/observational-memory/processor.d.ts +70 -0
- package/dist/processors/observational-memory/processor.d.ts.map +1 -0
- package/dist/processors/observational-memory/reflector-agent.d.ts +62 -0
- package/dist/processors/observational-memory/reflector-agent.d.ts.map +1 -0
- package/dist/processors/observational-memory/reflector-runner.d.ts +133 -0
- package/dist/processors/observational-memory/reflector-runner.d.ts.map +1 -0
- package/dist/processors/observational-memory/repro-capture.d.ts +33 -0
- package/dist/processors/observational-memory/repro-capture.d.ts.map +1 -0
- package/dist/processors/observational-memory/retry.d.ts +63 -0
- package/dist/processors/observational-memory/retry.d.ts.map +1 -0
- package/dist/processors/observational-memory/string-utils.d.ts +13 -0
- package/dist/processors/observational-memory/string-utils.d.ts.map +1 -0
- package/dist/processors/observational-memory/summarize.d.ts +92 -0
- package/dist/processors/observational-memory/summarize.d.ts.map +1 -0
- package/dist/processors/observational-memory/temporal-markers.d.ts +4 -0
- package/dist/processors/observational-memory/temporal-markers.d.ts.map +1 -0
- package/dist/processors/observational-memory/temporary-memory.d.ts +8 -0
- package/dist/processors/observational-memory/temporary-memory.d.ts.map +1 -0
- package/dist/processors/observational-memory/thresholds.d.ts +52 -0
- package/dist/processors/observational-memory/thresholds.d.ts.map +1 -0
- package/dist/processors/observational-memory/token-counter.d.ts +57 -0
- package/dist/processors/observational-memory/token-counter.d.ts.map +1 -0
- package/dist/processors/observational-memory/tool-result-helpers.d.ts +12 -0
- package/dist/processors/observational-memory/tool-result-helpers.d.ts.map +1 -0
- package/dist/processors/observational-memory/tracing.d.ts +17 -0
- package/dist/processors/observational-memory/tracing.d.ts.map +1 -0
- package/dist/processors/observational-memory/types.d.ts +997 -0
- package/dist/processors/observational-memory/types.d.ts.map +1 -0
- package/dist/processors/observational-memory/working-memory-extractor.d.ts +5 -0
- package/dist/processors/observational-memory/working-memory-extractor.d.ts.map +1 -0
- package/dist/processors/working-memory-state/index.d.ts +2 -0
- package/dist/processors/working-memory-state/index.d.ts.map +1 -0
- package/dist/processors/working-memory-state/processor.d.ts +53 -0
- package/dist/processors/working-memory-state/processor.d.ts.map +1 -0
- package/dist/src-C1tlhGeW.js +28559 -0
- package/dist/src-C1tlhGeW.js.map +1 -0
- package/dist/src-DqoifKIy.cjs +28813 -0
- package/dist/src-DqoifKIy.cjs.map +1 -0
- package/dist/tools/om-tools.d.ts +172 -0
- package/dist/tools/om-tools.d.ts.map +1 -0
- package/dist/tools/working-memory.d.ts +33 -28
- package/dist/tools/working-memory.d.ts.map +1 -1
- package/package.json +36 -28
- package/dist/index.cjs.map +0 -1
- package/dist/index.js.map +0 -1
- package/dist/processors/index.cjs.map +0 -1
- package/dist/processors/index.js.map +0 -1
- package/dist/processors/token-limiter.d.ts +0 -32
- package/dist/processors/token-limiter.d.ts.map +0 -1
- package/dist/processors/tool-call-filter.d.ts +0 -20
- package/dist/processors/tool-call-filter.d.ts.map +0 -1
|
@@ -0,0 +1,567 @@
|
|
|
1
|
+
> Discover all available pages from the documentation index: https://mastra.ai/llms.txt
|
|
2
|
+
|
|
3
|
+
# MongoDB vector store
|
|
4
|
+
|
|
5
|
+
The `MongoDBVector` class provides vector search using [MongoDB Atlas Vector Search](https://www.mongodb.com/docs/atlas/atlas-vector-search/). It enables efficient similarity search and metadata filtering within your MongoDB collections.
|
|
6
|
+
|
|
7
|
+
## Installation
|
|
8
|
+
|
|
9
|
+
**npm**:
|
|
10
|
+
|
|
11
|
+
```bash
|
|
12
|
+
npm install @mastra/mongodb@latest
|
|
13
|
+
```
|
|
14
|
+
|
|
15
|
+
**pnpm**:
|
|
16
|
+
|
|
17
|
+
```bash
|
|
18
|
+
pnpm add @mastra/mongodb@latest
|
|
19
|
+
```
|
|
20
|
+
|
|
21
|
+
**Yarn**:
|
|
22
|
+
|
|
23
|
+
```bash
|
|
24
|
+
yarn add @mastra/mongodb@latest
|
|
25
|
+
```
|
|
26
|
+
|
|
27
|
+
**Bun**:
|
|
28
|
+
|
|
29
|
+
```bash
|
|
30
|
+
bun add @mastra/mongodb@latest
|
|
31
|
+
```
|
|
32
|
+
|
|
33
|
+
## Usage example
|
|
34
|
+
|
|
35
|
+
```typescript
|
|
36
|
+
import { MongoDBVector } from '@mastra/mongodb'
|
|
37
|
+
|
|
38
|
+
const store = new MongoDBVector({
|
|
39
|
+
id: 'mongodb-vector',
|
|
40
|
+
uri: process.env.MONGODB_URI,
|
|
41
|
+
dbName: process.env.MONGODB_DB_NAME,
|
|
42
|
+
})
|
|
43
|
+
```
|
|
44
|
+
|
|
45
|
+
### Custom Embedding Field Path
|
|
46
|
+
|
|
47
|
+
If you need to store embeddings in a nested field structure (e.g., to integrate with existing MongoDB collections), use the `embeddingFieldPath` option:
|
|
48
|
+
|
|
49
|
+
```typescript
|
|
50
|
+
import { MongoDBVector } from '@mastra/mongodb'
|
|
51
|
+
|
|
52
|
+
const store = new MongoDBVector({
|
|
53
|
+
id: 'mongodb-vector',
|
|
54
|
+
uri: process.env.MONGODB_URI,
|
|
55
|
+
dbName: process.env.MONGODB_DB_NAME,
|
|
56
|
+
embeddingFieldPath: 'text.contentEmbedding', // Store embeddings at text.contentEmbedding
|
|
57
|
+
})
|
|
58
|
+
```
|
|
59
|
+
|
|
60
|
+
## Constructor options
|
|
61
|
+
|
|
62
|
+
**id** (`string`): Unique identifier for this vector store instance
|
|
63
|
+
|
|
64
|
+
**uri** (`string`): MongoDB connection string
|
|
65
|
+
|
|
66
|
+
**dbName** (`string`): Name of the MongoDB database to use
|
|
67
|
+
|
|
68
|
+
**options** (`MongoClientOptions`): Optional MongoDB client options
|
|
69
|
+
|
|
70
|
+
**embeddingFieldPath** (`string`): Path to the field that stores vector embeddings. Supports nested paths using dot notation (e.g., 'text.contentEmbedding'). (Default: `embedding`)
|
|
71
|
+
|
|
72
|
+
## Methods
|
|
73
|
+
|
|
74
|
+
### `connect()`
|
|
75
|
+
|
|
76
|
+
Establishes connection to the MongoDB server. This is called automatically on first use, but can be called explicitly if needed.
|
|
77
|
+
|
|
78
|
+
```typescript
|
|
79
|
+
await store.connect()
|
|
80
|
+
```
|
|
81
|
+
|
|
82
|
+
### `createIndex()`
|
|
83
|
+
|
|
84
|
+
Creates a new vector index (collection) in MongoDB.
|
|
85
|
+
|
|
86
|
+
**indexName** (`string`): Name of the collection to create
|
|
87
|
+
|
|
88
|
+
**dimension** (`number`): Vector dimension (must match your embedding model)
|
|
89
|
+
|
|
90
|
+
**metric** (`'cosine' | 'euclidean' | 'dotproduct'`): Distance metric for similarity search (Default: `cosine`)
|
|
91
|
+
|
|
92
|
+
**filterFields** (`string[]`): Metadata field names to declare as filter fields in the Atlas vectorSearch index (registered as metadata.\<field>). Queries that filter only on declared fields are pushed directly into $vectorSearch instead of pre-filtering candidate \_ids, avoiding the 16 MB BSON limit on large result sets. Filters that reference an undeclared field, or use an operator $vectorSearch does not support, fall back to the pre-filter automatically.
|
|
93
|
+
|
|
94
|
+
**collectionName** (`string`): Store the vectors on an existing (operational) collection instead of a managed collection named after the index. The collection is never created or dropped by this store when set. Defaults to indexName.
|
|
95
|
+
|
|
96
|
+
**searchIndexName** (`string`): Name for the Atlas vectorSearch index created on the collection. Defaults to ${indexName}\_vector\_index.
|
|
97
|
+
|
|
98
|
+
**allowWrites** (`boolean`): Opt-in to write operations (upsert, updateVector, deleteVector, deleteVectors) on a bring-your-own collection. By default a BYO index is read-only: the store never modifies or deletes caller-owned operational documents. Ignored for managed collections, which are always writable. The policy is persisted with the index registration and survives restarts. (Default: `false`)
|
|
99
|
+
|
|
100
|
+
### `waitForIndexReady()`
|
|
101
|
+
|
|
102
|
+
Waits for an index to become ready after creation. Useful when you need to ensure an index is ready before performing operations.
|
|
103
|
+
|
|
104
|
+
**indexName** (`string`): Name of the index to wait for
|
|
105
|
+
|
|
106
|
+
**timeoutMs** (`number`): Maximum time to wait in milliseconds (Default: `60000`)
|
|
107
|
+
|
|
108
|
+
**checkIntervalMs** (`number`): Interval between status checks in milliseconds (Default: `2000`)
|
|
109
|
+
|
|
110
|
+
### `upsert()`
|
|
111
|
+
|
|
112
|
+
Adds or updates vectors and their metadata in the collection. On a bring-your-own index, this requires `allowWrites: true` at `createIndex()` time because BYO collections are read-only by default.
|
|
113
|
+
|
|
114
|
+
**indexName** (`string`): Name of the collection to insert into
|
|
115
|
+
|
|
116
|
+
**vectors** (`number[][]`): Array of embedding vectors
|
|
117
|
+
|
|
118
|
+
**metadata** (`Record<string, any>[]`): Metadata for each vector
|
|
119
|
+
|
|
120
|
+
**ids** (`string[]`): Optional vector IDs (auto-generated if not provided)
|
|
121
|
+
|
|
122
|
+
**documents** (`string[]`): Optional document text content to store alongside vectors
|
|
123
|
+
|
|
124
|
+
### `query()`
|
|
125
|
+
|
|
126
|
+
Searches for similar vectors with optional metadata filtering.
|
|
127
|
+
|
|
128
|
+
**indexName** (`string`): Name of the collection to search in
|
|
129
|
+
|
|
130
|
+
**queryVector** (`number[]`): Query vector to find similar vectors for
|
|
131
|
+
|
|
132
|
+
**topK** (`number`): Number of results to return (Default: `10`)
|
|
133
|
+
|
|
134
|
+
**filter** (`Record<string, any>`): Metadata filters (applies to the metadata field)
|
|
135
|
+
|
|
136
|
+
**documentFilter** (`Record<string, any>`): Filters on original document fields (not just metadata)
|
|
137
|
+
|
|
138
|
+
**includeVector** (`boolean`): Whether to include vector data in results (Default: `false`)
|
|
139
|
+
|
|
140
|
+
**numCandidates** (`number`): Number of candidates the HNSW graph considers before selecting top-K results. Higher values improve recall at the cost of latency. See: https\://www\.mongodb.com/docs/atlas/atlas-vector-search/vector-search-stage/ (Default: `20 * topK (capped at 10000)`)
|
|
141
|
+
|
|
142
|
+
**metadataMode** (`'field' | 'document'`): 'field' (default) projects the managed metadata/document fields, and filter fields are matched against the metadata subdocument. 'document' returns the full source document as metadata — use for bring-your-own operational collections whose documents have their own shape — and filter fields are matched against the \*\*root\*\* document (no metadata. prefix). The embedding field is omitted from metadata by default (to avoid payload bloat); set includeVector: true to retain it in metadata and also expose it as a top-level vector. (Default: `field`)
|
|
143
|
+
|
|
144
|
+
### `createSearchIndex()`
|
|
145
|
+
|
|
146
|
+
Provisions an Atlas Search (BM25/full-text) index on the collection backing an index and records it as the text-search index that `textQuery()` and `hybridQuery()` will target.
|
|
147
|
+
|
|
148
|
+
**Managed vs. bring-your-own collections:**
|
|
149
|
+
|
|
150
|
+
- For a **managed** index (created without `collectionName`), `createIndex()` already provisions a _dynamic_ full-text index named `${collectionName}_search_index` (covering all string fields). `createSearchIndex()` is therefore only needed when you want a **field-restricted** mapping or a **custom index name**.
|
|
151
|
+
- For a **bring-your-own** index (created with `collectionName`), `createIndex()` doesn't auto-create any full-text index. Enabling `textQuery()`/`hybridQuery()` on a caller-owned operational collection is opt-in. Call `createSearchIndex()` explicitly to provision the (billable) text index. Until you do, `textQuery()`/`hybridQuery()` throw a clear error rather than querying a non-existent index.
|
|
152
|
+
|
|
153
|
+
Naming:
|
|
154
|
+
|
|
155
|
+
- When `fields` is provided **without** an explicit `searchIndexName`, the field-mapped index is created under a **distinct** default name (`${collectionName}_${indexName}_search_fields_index`, unique per logical index) so it doesn't collide with a managed collection's auto-created dynamic index and get silently ignored. This distinct index is persisted as the text-search index, so `textQuery()`/`hybridQuery()` use the restricted mapping automatically.
|
|
156
|
+
- When `searchIndexName` is provided, that exact name is used and persisted. `textQuery()`/`hybridQuery()` resolve the persisted name automatically. You can also override the name per call via their `searchIndexName` / `textSearchIndexName` parameters.
|
|
157
|
+
|
|
158
|
+
**indexName** (`string`): Name of the Mastra index whose collection will have the search index
|
|
159
|
+
|
|
160
|
+
**fields** (`string[]`): Field names to index for full-text search. Omit for dynamic mapping (all string fields).
|
|
161
|
+
|
|
162
|
+
**searchIndexName** (`string`): Name for the Atlas Search index. When fields is provided and this is omitted, a distinct default name that is unique per logical index is used, so the field mapping is not shadowed by the auto-created dynamic index and two logical indexes on the same collection do not collide. (Default: ``${collectionName}_search_index (or ${collectionName}_${indexName}_search_fields_index when `fields` is given)``)
|
|
163
|
+
|
|
164
|
+
**waitUntilReady** (`boolean`): When true, block until the provisioned full-text index reports READY before resolving. Defaults to false to avoid surprising latency; call waitForSearchIndexReady() explicitly if you prefer to await separately. (Default: `false`)
|
|
165
|
+
|
|
166
|
+
```typescript
|
|
167
|
+
await store.createSearchIndex({
|
|
168
|
+
indexName: 'precedents',
|
|
169
|
+
fields: ['note', 'description'],
|
|
170
|
+
})
|
|
171
|
+
```
|
|
172
|
+
|
|
173
|
+
The field-mapped index name includes the logical `indexName`, so two logical indexes on the same collection get distinct text indexes. Recreating the _same_ logical index with different `fields` still requires dropping the existing index first (`IndexAlreadyExists`).
|
|
174
|
+
|
|
175
|
+
### `waitForSearchIndexReady()`
|
|
176
|
+
|
|
177
|
+
Waits for the full-text (BM25) search index of an index to become READY. `waitForIndexReady()` polls only the vectorSearch index; `createSearchIndex()` returns while the Atlas Search full-text index is still building, so an immediate `textQuery()`/`hybridQuery()` can intermittently fail. Call this (or pass `waitUntilReady: true` to `createSearchIndex()`) to block until the resolved text index reports READY.
|
|
178
|
+
|
|
179
|
+
**indexName** (`string`): Logical name of the index whose text index to wait for
|
|
180
|
+
|
|
181
|
+
**searchIndexName** (`string`): Override the resolved text-search index name
|
|
182
|
+
|
|
183
|
+
**timeoutMs** (`number`): Maximum time to wait in milliseconds (Default: `60000`)
|
|
184
|
+
|
|
185
|
+
**checkIntervalMs** (`number`): Interval between status checks in milliseconds (Default: `2000`)
|
|
186
|
+
|
|
187
|
+
```typescript
|
|
188
|
+
await store.createSearchIndex({ indexName: 'precedents', fields: ['note'] })
|
|
189
|
+
await store.waitForSearchIndexReady({ indexName: 'precedents' })
|
|
190
|
+
```
|
|
191
|
+
|
|
192
|
+
### `textQuery()`
|
|
193
|
+
|
|
194
|
+
Runs a full-text (BM25) search against an Atlas Search index. By default it targets the text-search index recorded for this index (set by `createSearchIndex()`, or the dynamic `${collectionName}_search_index` auto-created by `createIndex()`). Pass `searchIndexName` to target a specific index for this call.
|
|
195
|
+
|
|
196
|
+
Metadata filters here (like `hybridQuery()`) are applied via a `$match` stage. For the vector branch of `hybridQuery()`, filters on fields not declared via `filterFields` at index creation are transparently materialised as candidate `_id`s (the same fallback `query()` uses), so undeclared-field filters don't error.
|
|
197
|
+
|
|
198
|
+
**indexName** (`string`): Name of the Mastra index to search
|
|
199
|
+
|
|
200
|
+
**query** (`string`): Full-text search query string
|
|
201
|
+
|
|
202
|
+
**paths** (`string[]`): Field paths to search in (e.g., \["note", "description"])
|
|
203
|
+
|
|
204
|
+
**topK** (`number`): Number of results to return (Default: `10`)
|
|
205
|
+
|
|
206
|
+
**filter** (`Record<string, any>`): Metadata filters (applies to the metadata field)
|
|
207
|
+
|
|
208
|
+
**metadataMode** (`'field' | 'document'`): 'field' (default) projects the managed metadata/document fields. 'document' returns the full source document as metadata. (Default: `field`)
|
|
209
|
+
|
|
210
|
+
**searchIndexName** (`string`): Override the resolved full-text search index name for this call. Defaults to the index persisted by createSearchIndex() / createIndex().
|
|
211
|
+
|
|
212
|
+
```typescript
|
|
213
|
+
const results = await store.textQuery({
|
|
214
|
+
indexName: 'precedents',
|
|
215
|
+
query: 'shell company offshore',
|
|
216
|
+
paths: ['note'],
|
|
217
|
+
topK: 10,
|
|
218
|
+
})
|
|
219
|
+
```
|
|
220
|
+
|
|
221
|
+
### `hybridQuery()`
|
|
222
|
+
|
|
223
|
+
Runs a hybrid search that fuses vector similarity and full-text results using MongoDB's server-side `$rankFusion`. It requires MongoDB >= 8.0 and is generally available from 8.1. On 8.0.x, it may need a MongoDB support case to enable, and it runs where enabled, such as Atlas 8.0.x. A full-text search index must exist: it's auto-created for managed indexes, but for a bring-your-own collection you must call `createSearchIndex()` first (opt-in).
|
|
224
|
+
|
|
225
|
+
**indexName** (`string`): Name of the Mastra index to search
|
|
226
|
+
|
|
227
|
+
**queryVector** (`number[]`): Query vector for similarity search
|
|
228
|
+
|
|
229
|
+
**query** (`string`): Full-text search query string
|
|
230
|
+
|
|
231
|
+
**paths** (`string[]`): Field paths to search in for full-text (e.g., \["note", "description"])
|
|
232
|
+
|
|
233
|
+
**topK** (`number`): Number of results to return (Default: `10`)
|
|
234
|
+
|
|
235
|
+
**filter** (`Record<string, any>`): Metadata filters (applies to both vector and text branches)
|
|
236
|
+
|
|
237
|
+
**weights** (`{ vector?: number; text?: number }`): Relative weights for vector vs. text results in fusion (default: 1:1)
|
|
238
|
+
|
|
239
|
+
**numCandidates** (`number`): Number of candidates for the vector search branch (Default: `20 * topK (capped at 10000)`)
|
|
240
|
+
|
|
241
|
+
**metadataMode** (`'field' | 'document'`): 'field' (default) projects the managed metadata/document fields. 'document' returns the full source document as metadata. (Default: `field`)
|
|
242
|
+
|
|
243
|
+
**textSearchIndexName** (`string`): Override the resolved full-text search index name for this call. Defaults to the index persisted by createSearchIndex() / createIndex().
|
|
244
|
+
|
|
245
|
+
```typescript
|
|
246
|
+
const results = await store.hybridQuery({
|
|
247
|
+
indexName: 'precedents',
|
|
248
|
+
queryVector: embedding,
|
|
249
|
+
query: 'shell company offshore',
|
|
250
|
+
paths: ['note'],
|
|
251
|
+
topK: 10,
|
|
252
|
+
weights: { vector: 1, text: 1.5 }, // Favor text matches
|
|
253
|
+
})
|
|
254
|
+
```
|
|
255
|
+
|
|
256
|
+
`hybridQuery()` requires MongoDB >= 8.0 for the `$rankFusion` stage. The stage is generally available from 8.1. On 8.0.x, it may need a MongoDB support case to enable and runs where enabled, such as Atlas 8.0.x. If you're running an older version, or `$rankFusion` isn't enabled on your 8.0.x deployment, use `query()` and `textQuery()` separately and merge the results client-side.
|
|
257
|
+
|
|
258
|
+
### `describeIndex()`
|
|
259
|
+
|
|
260
|
+
Returns information about the index (collection).
|
|
261
|
+
|
|
262
|
+
**indexName** (`string`): Name of the collection to describe
|
|
263
|
+
|
|
264
|
+
Returns:
|
|
265
|
+
|
|
266
|
+
```typescript
|
|
267
|
+
interface IndexStats {
|
|
268
|
+
dimension: number
|
|
269
|
+
count: number
|
|
270
|
+
metric: 'cosine' | 'euclidean' | 'dotproduct'
|
|
271
|
+
}
|
|
272
|
+
```
|
|
273
|
+
|
|
274
|
+
### `deleteIndex()`
|
|
275
|
+
|
|
276
|
+
Deletes a vector index. Behavior depends on how the index was created:
|
|
277
|
+
|
|
278
|
+
- **Managed index** (created without `collectionName`): drops the entire collection and all its data.
|
|
279
|
+
- **Bring-your-own index** (created with `collectionName`): drops the Atlas vectorSearch index and, if one was provisioned via `createSearchIndex()`, the companion full-text search index. The caller's operational collection and its documents are preserved. This store never drops a collection it didn't create.
|
|
280
|
+
|
|
281
|
+
The BYO classification is recorded durably when the index is created, so it's applied correctly even by a different process (e.g. an index created by a setup job and later deleted by a long-lived service). Always pass the **logical index name** (the `indexName` used at `createIndex`), not the physical collection name.
|
|
282
|
+
|
|
283
|
+
**indexName** (`string`): Logical name of the index to delete
|
|
284
|
+
|
|
285
|
+
### `listIndexes()`
|
|
286
|
+
|
|
287
|
+
Lists the **logical** Mastra index names (the `indexName` values passed to `createIndex`), not physical collection names. For a bring-your-own index whose data lives in an operational collection, the logical index name is returned instead of the physical collection name. The value can be passed straight back into `deleteIndex()` / `describeIndex()`. Managed indexes created before durable metadata was introduced are still discovered via their `${name}_vector_index` search index. The internal registry collection is never listed.
|
|
288
|
+
|
|
289
|
+
Returns: `Promise<string[]>`
|
|
290
|
+
|
|
291
|
+
### `updateVector()`
|
|
292
|
+
|
|
293
|
+
Update a single vector by ID or by metadata filter. Either `id` or `filter` must be provided, but not both.
|
|
294
|
+
|
|
295
|
+
> **Bring-your-own collections are read-only by default.** `upsert()`, `updateVector()`, `deleteVector()`, and `deleteVectors()` throw a USER-category error on a BYO index unless it was created with `allowWrites: true`. See [Indexing an existing collection](#indexing-an-existing-collection).
|
|
296
|
+
|
|
297
|
+
**indexName** (`string`): Name of the collection containing the vector
|
|
298
|
+
|
|
299
|
+
**id** (`string`): ID of the vector entry to update (mutually exclusive with filter)
|
|
300
|
+
|
|
301
|
+
**filter** (`Record<string, any>`): Metadata filter to identify vector(s) to update (mutually exclusive with id)
|
|
302
|
+
|
|
303
|
+
**update** (`object`): Update data containing vector and/or metadata
|
|
304
|
+
|
|
305
|
+
**update.vector** (`number[]`): New vector data to update
|
|
306
|
+
|
|
307
|
+
**update.metadata** (`Record<string, any>`): New metadata to update
|
|
308
|
+
|
|
309
|
+
### `deleteVector()`
|
|
310
|
+
|
|
311
|
+
Deletes a specific vector entry from an index by its ID.
|
|
312
|
+
|
|
313
|
+
**indexName** (`string`): Name of the collection containing the vector
|
|
314
|
+
|
|
315
|
+
**id** (`string`): ID of the vector entry to delete
|
|
316
|
+
|
|
317
|
+
### `deleteVectors()`
|
|
318
|
+
|
|
319
|
+
Delete multiple vectors by IDs or by metadata filter. Either `ids` or `filter` must be provided, but not both.
|
|
320
|
+
|
|
321
|
+
**indexName** (`string`): Name of the collection containing the vectors to delete
|
|
322
|
+
|
|
323
|
+
**ids** (`string[]`): Array of vector IDs to delete (mutually exclusive with filter)
|
|
324
|
+
|
|
325
|
+
**filter** (`Record<string, any>`): Metadata filter to identify vectors to delete (mutually exclusive with ids)
|
|
326
|
+
|
|
327
|
+
### `disconnect()`
|
|
328
|
+
|
|
329
|
+
Closes the MongoDB client connection. Should be called when done using the store.
|
|
330
|
+
|
|
331
|
+
## Response types
|
|
332
|
+
|
|
333
|
+
Query results are returned in this format:
|
|
334
|
+
|
|
335
|
+
```typescript
|
|
336
|
+
interface QueryResult {
|
|
337
|
+
id: string
|
|
338
|
+
score: number
|
|
339
|
+
metadata: Record<string, any>
|
|
340
|
+
vector?: number[] // Only included if includeVector is true
|
|
341
|
+
}
|
|
342
|
+
```
|
|
343
|
+
|
|
344
|
+
## Error handling
|
|
345
|
+
|
|
346
|
+
The store throws typed errors that can be caught:
|
|
347
|
+
|
|
348
|
+
```typescript
|
|
349
|
+
try {
|
|
350
|
+
await store.query({
|
|
351
|
+
indexName: 'my_collection',
|
|
352
|
+
queryVector: queryVector,
|
|
353
|
+
})
|
|
354
|
+
} catch (error) {
|
|
355
|
+
// Handle specific error cases
|
|
356
|
+
if (error.message.includes('Invalid collection name')) {
|
|
357
|
+
console.error(
|
|
358
|
+
'Collection name must start with a letter or underscore and contain only valid characters.',
|
|
359
|
+
)
|
|
360
|
+
} else if (error.message.includes('Collection not found')) {
|
|
361
|
+
console.error('The specified collection does not exist')
|
|
362
|
+
} else {
|
|
363
|
+
console.error('Vector store error:', error.message)
|
|
364
|
+
}
|
|
365
|
+
}
|
|
366
|
+
```
|
|
367
|
+
|
|
368
|
+
## Indexing an existing collection
|
|
369
|
+
|
|
370
|
+
You can create a vector index on an existing operational collection instead of using a managed collection. This is useful when you want to add vector search capabilities to documents that already exist in your MongoDB database.
|
|
371
|
+
|
|
372
|
+
```typescript
|
|
373
|
+
import { MongoDBVector } from '@mastra/mongodb'
|
|
374
|
+
|
|
375
|
+
const store = new MongoDBVector({
|
|
376
|
+
id: 'mongodb-vector',
|
|
377
|
+
uri: process.env.MONGODB_URI,
|
|
378
|
+
dbName: process.env.MONGODB_DB_NAME,
|
|
379
|
+
})
|
|
380
|
+
|
|
381
|
+
// Create a vector index on an existing 'transactions' collection
|
|
382
|
+
await store.createIndex({
|
|
383
|
+
indexName: 'precedents',
|
|
384
|
+
dimension: 1024,
|
|
385
|
+
collectionName: 'transactions', // Use existing collection
|
|
386
|
+
searchIndexName: 'txn_vec_idx', // Custom search index name
|
|
387
|
+
})
|
|
388
|
+
|
|
389
|
+
// Wait for the index to be ready
|
|
390
|
+
await store.waitForIndexReady({ indexName: 'precedents' })
|
|
391
|
+
|
|
392
|
+
// Query using document mode to get full source documents
|
|
393
|
+
const hits = await store.query({
|
|
394
|
+
indexName: 'precedents',
|
|
395
|
+
queryVector: embeddings,
|
|
396
|
+
topK: 5,
|
|
397
|
+
metadataMode: 'document', // Returns full document as metadata
|
|
398
|
+
})
|
|
399
|
+
|
|
400
|
+
// hits[0].metadata now contains all fields from the source document
|
|
401
|
+
console.log(hits[0].metadata.amount, hits[0].metadata.customField)
|
|
402
|
+
|
|
403
|
+
// Full-text / hybrid search on a BYO collection is opt-in: provision the text index first.
|
|
404
|
+
await store.createSearchIndex({ indexName: 'precedents', fields: ['note'] })
|
|
405
|
+
```
|
|
406
|
+
|
|
407
|
+
**Important notes:**
|
|
408
|
+
|
|
409
|
+
- The collection must already exist and contain documents with an `embedding` field (or the custom `embeddingFieldPath` you configured)
|
|
410
|
+
- The collection is never created or dropped when using `collectionName`
|
|
411
|
+
- **A BYO index is read-only by default.** `upsert()`, `updateVector()`, `deleteVector()`, and `deleteVectors()` throw a clear error rather than mutating caller-owned operational documents. To let the store write embeddings into (or delete documents from) your collection, opt in explicitly with `createIndex({ ..., allowWrites: true })`. The policy is persisted and survives restarts. Entries written by older versions without the flag are treated as read-only (fail closed).
|
|
412
|
+
- Use `metadataMode: 'document'` when querying to retrieve the full source document as `metadata`
|
|
413
|
+
- In `'document'` mode the embedding is omitted from `metadata` by default; pass `includeVector: true` to retain it (and also expose it as a top-level `vector`)
|
|
414
|
+
- **Filtering in `'document'` mode operates on root document fields**, not a nested `metadata.` subdocument. `filter: { lane: 'fraud' }` matches the top-level `lane` field of your operational documents (in the default `'field'` mode, bare fields are rewritten to `metadata.<field>` for managed collections). Both the pushdown and `$match` fallback paths honor this.
|
|
415
|
+
- **Native `ObjectId` `_id`s are supported.** Operational collections commonly key on `ObjectId`; query results coerce `_id` to a string (the `QueryResult.id` contract), and `deleteVector()`/`updateVector()`/`deleteVectors()` accept that string and match the underlying `ObjectId` document. Managed collections (string `_id`s) are unaffected.
|
|
416
|
+
- Full-text and hybrid search on a BYO collection are **opt-in**: no full-text index is auto-created, so call `createSearchIndex()` before `textQuery()`/`hybridQuery()`. The full-text index builds asynchronously. Call `waitForSearchIndexReady()` (or pass `waitUntilReady: true`) before an immediate text/hybrid query.
|
|
417
|
+
- `deleteIndex()` on a BYO index drops the vector index (and the text index if one was created) but **preserves** the collection and its documents
|
|
418
|
+
|
|
419
|
+
## Best practices
|
|
420
|
+
|
|
421
|
+
- Index metadata fields used in filters for optimal query performance.
|
|
422
|
+
- Use consistent field naming in metadata to avoid unexpected query results.
|
|
423
|
+
- Regularly monitor index and collection statistics to ensure efficient search.
|
|
424
|
+
- When indexing existing collections, ensure all documents have the required `embedding` field.
|
|
425
|
+
|
|
426
|
+
## Usage example
|
|
427
|
+
|
|
428
|
+
### Vector embeddings with `MongoDB`
|
|
429
|
+
|
|
430
|
+
Embeddings are numeric vectors used by memory's `semanticRecall` to retrieve related messages by meaning (not keywords).
|
|
431
|
+
|
|
432
|
+
> **Note:** MongoDB Atlas Vector Search is recommended for production use. For self-hosted deployments, Vector Search is available with [local Atlas deployments via the Atlas CLI](https://www.mongodb.com/docs/atlas/cli/current/atlas-cli-deploy-local/).
|
|
433
|
+
|
|
434
|
+
This setup uses FastEmbed, a local embedding model, to generate vector embeddings. To use this, install `@mastra/fastembed`:
|
|
435
|
+
|
|
436
|
+
**npm**:
|
|
437
|
+
|
|
438
|
+
```bash
|
|
439
|
+
npm install @mastra/fastembed@latest
|
|
440
|
+
```
|
|
441
|
+
|
|
442
|
+
**pnpm**:
|
|
443
|
+
|
|
444
|
+
```bash
|
|
445
|
+
pnpm add @mastra/fastembed@latest
|
|
446
|
+
```
|
|
447
|
+
|
|
448
|
+
**Yarn**:
|
|
449
|
+
|
|
450
|
+
```bash
|
|
451
|
+
yarn add @mastra/fastembed@latest
|
|
452
|
+
```
|
|
453
|
+
|
|
454
|
+
**Bun**:
|
|
455
|
+
|
|
456
|
+
```bash
|
|
457
|
+
bun add @mastra/fastembed@latest
|
|
458
|
+
```
|
|
459
|
+
|
|
460
|
+
Add the following to your agent:
|
|
461
|
+
|
|
462
|
+
```typescript
|
|
463
|
+
import { Memory } from '@mastra/memory'
|
|
464
|
+
import { Agent } from '@mastra/core/agent'
|
|
465
|
+
import { MongoDBStore, MongoDBVector } from '@mastra/mongodb'
|
|
466
|
+
import { fastembed } from '@mastra/fastembed'
|
|
467
|
+
|
|
468
|
+
export const mongodbAgent = new Agent({
|
|
469
|
+
id: 'mongodb-agent',
|
|
470
|
+
name: 'mongodb-agent',
|
|
471
|
+
instructions:
|
|
472
|
+
'You are an AI agent with the ability to automatically recall memories from previous interactions.',
|
|
473
|
+
model: 'openai/gpt-5.6-sol',
|
|
474
|
+
memory: new Memory({
|
|
475
|
+
storage: new MongoDBStore({
|
|
476
|
+
id: 'mongodb-storage',
|
|
477
|
+
uri: process.env.MONGODB_URI!,
|
|
478
|
+
dbName: process.env.MONGODB_DB_NAME!,
|
|
479
|
+
}),
|
|
480
|
+
vector: new MongoDBVector({
|
|
481
|
+
id: 'mongodb-vector',
|
|
482
|
+
uri: process.env.MONGODB_URI!,
|
|
483
|
+
dbName: process.env.MONGODB_DB_NAME!,
|
|
484
|
+
}),
|
|
485
|
+
embedder: fastembed,
|
|
486
|
+
options: {
|
|
487
|
+
lastMessages: 10,
|
|
488
|
+
semanticRecall: {
|
|
489
|
+
topK: 3,
|
|
490
|
+
messageRange: 2,
|
|
491
|
+
},
|
|
492
|
+
generateTitle: true, // generates descriptive thread titles automatically
|
|
493
|
+
},
|
|
494
|
+
}),
|
|
495
|
+
})
|
|
496
|
+
```
|
|
497
|
+
|
|
498
|
+
### Vector embeddings with VoyageAI
|
|
499
|
+
|
|
500
|
+
VoyageAI provides specialized embedding models optimized for retrieval tasks. VoyageAI is also integrated with MongoDB Atlas for multimodal embeddings.
|
|
501
|
+
|
|
502
|
+
**npm**:
|
|
503
|
+
|
|
504
|
+
```bash
|
|
505
|
+
npm install @mastra/voyageai@latest
|
|
506
|
+
```
|
|
507
|
+
|
|
508
|
+
**pnpm**:
|
|
509
|
+
|
|
510
|
+
```bash
|
|
511
|
+
pnpm add @mastra/voyageai@latest
|
|
512
|
+
```
|
|
513
|
+
|
|
514
|
+
**Yarn**:
|
|
515
|
+
|
|
516
|
+
```bash
|
|
517
|
+
yarn add @mastra/voyageai@latest
|
|
518
|
+
```
|
|
519
|
+
|
|
520
|
+
**Bun**:
|
|
521
|
+
|
|
522
|
+
```bash
|
|
523
|
+
bun add @mastra/voyageai@latest
|
|
524
|
+
```
|
|
525
|
+
|
|
526
|
+
Basic usage example:
|
|
527
|
+
|
|
528
|
+
```typescript
|
|
529
|
+
import { Memory } from '@mastra/memory'
|
|
530
|
+
import { Agent } from '@mastra/core/agent'
|
|
531
|
+
import { MongoDBStore, MongoDBVector } from '@mastra/mongodb'
|
|
532
|
+
import { voyage } from '@mastra/voyageai'
|
|
533
|
+
|
|
534
|
+
export const mongodbVoyageAgent = new Agent({
|
|
535
|
+
id: 'mongodb-voyage-agent',
|
|
536
|
+
name: 'MongoDB VoyageAI Agent',
|
|
537
|
+
instructions: 'You are an AI agent with semantic recall powered by VoyageAI and MongoDB.',
|
|
538
|
+
model: 'openai/gpt-5.6-sol',
|
|
539
|
+
memory: new Memory({
|
|
540
|
+
storage: new MongoDBStore({
|
|
541
|
+
id: 'mongodb-storage',
|
|
542
|
+
uri: process.env.MONGODB_URI!,
|
|
543
|
+
dbName: process.env.MONGODB_DB_NAME!,
|
|
544
|
+
}),
|
|
545
|
+
vector: new MongoDBVector({
|
|
546
|
+
id: 'mongodb-vector',
|
|
547
|
+
uri: process.env.MONGODB_URI!,
|
|
548
|
+
dbName: process.env.MONGODB_DB_NAME!,
|
|
549
|
+
}),
|
|
550
|
+
embedder: voyage, // VoyageAI's default model (voyage-3.5, 1024 dimensions)
|
|
551
|
+
options: {
|
|
552
|
+
lastMessages: 10,
|
|
553
|
+
semanticRecall: {
|
|
554
|
+
topK: 5,
|
|
555
|
+
messageRange: 2,
|
|
556
|
+
},
|
|
557
|
+
},
|
|
558
|
+
}),
|
|
559
|
+
})
|
|
560
|
+
```
|
|
561
|
+
|
|
562
|
+
For detailed VoyageAI embedding examples including specialized models, multimodal embeddings, and retrieval optimization, see the [VoyageAI embeddings documentation](https://mastra.ai/models/embeddings).
|
|
563
|
+
|
|
564
|
+
## Related
|
|
565
|
+
|
|
566
|
+
- [Metadata Filters](https://mastra.ai/reference/rag/metadata-filters)
|
|
567
|
+
- [VoyageAI Embeddings Documentation](https://mastra.ai/models/embeddings)
|