@mastra/memory 1.5.1 → 1.5.2
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 +28 -0
- package/dist/{chunk-6PKWQ3GH.js → chunk-HNPAIFCZ.js} +59 -16
- package/dist/chunk-HNPAIFCZ.js.map +1 -0
- package/dist/{chunk-6XVTMLW4.cjs → chunk-PVFLHAZX.cjs} +59 -16
- package/dist/chunk-PVFLHAZX.cjs.map +1 -0
- package/dist/docs/SKILL.md +55 -0
- package/dist/docs/assets/SOURCE_MAP.json +103 -0
- package/dist/docs/references/docs-agents-agent-approval.md +558 -0
- package/dist/docs/references/docs-agents-agent-memory.md +209 -0
- package/dist/docs/references/docs-agents-network-approval.md +275 -0
- package/dist/docs/references/docs-agents-networks.md +299 -0
- package/dist/docs/references/docs-agents-supervisor-agents.md +304 -0
- package/dist/docs/references/docs-memory-memory-processors.md +314 -0
- package/dist/docs/references/docs-memory-message-history.md +260 -0
- package/dist/docs/references/docs-memory-observational-memory.md +248 -0
- package/dist/docs/references/docs-memory-overview.md +45 -0
- package/dist/docs/references/docs-memory-semantic-recall.md +272 -0
- package/dist/docs/references/docs-memory-storage.md +261 -0
- package/dist/docs/references/docs-memory-working-memory.md +400 -0
- package/dist/docs/references/reference-core-getMemory.md +50 -0
- package/dist/docs/references/reference-core-listMemory.md +56 -0
- package/dist/docs/references/reference-memory-clone-utilities.md +199 -0
- package/dist/docs/references/reference-memory-cloneThread.md +130 -0
- package/dist/docs/references/reference-memory-createThread.md +68 -0
- package/dist/docs/references/reference-memory-getThreadById.md +24 -0
- package/dist/docs/references/reference-memory-listThreads.md +145 -0
- package/dist/docs/references/reference-memory-memory-class.md +147 -0
- package/dist/docs/references/reference-memory-observational-memory.md +565 -0
- package/dist/docs/references/reference-processors-token-limiter-processor.md +115 -0
- package/dist/docs/references/reference-storage-dynamodb.md +282 -0
- package/dist/docs/references/reference-storage-libsql.md +135 -0
- package/dist/docs/references/reference-storage-mongodb.md +262 -0
- package/dist/docs/references/reference-storage-postgresql.md +526 -0
- package/dist/docs/references/reference-storage-upstash.md +160 -0
- package/dist/docs/references/reference-vectors-libsql.md +305 -0
- package/dist/docs/references/reference-vectors-mongodb.md +295 -0
- package/dist/docs/references/reference-vectors-pg.md +408 -0
- package/dist/docs/references/reference-vectors-upstash.md +294 -0
- package/dist/index.cjs +1 -1
- package/dist/index.js +1 -1
- package/dist/{observational-memory-AJWSMZVP.js → observational-memory-KAFD4QZK.js} +3 -3
- package/dist/{observational-memory-AJWSMZVP.js.map → observational-memory-KAFD4QZK.js.map} +1 -1
- package/dist/{observational-memory-Q5TO525O.cjs → observational-memory-Q47HN5YL.cjs} +17 -17
- package/dist/{observational-memory-Q5TO525O.cjs.map → observational-memory-Q47HN5YL.cjs.map} +1 -1
- package/dist/processors/index.cjs +15 -15
- package/dist/processors/index.js +1 -1
- package/dist/processors/observational-memory/observational-memory.d.ts +2 -2
- package/dist/processors/observational-memory/observational-memory.d.ts.map +1 -1
- package/dist/processors/observational-memory/token-counter.d.ts.map +1 -1
- package/package.json +8 -8
- package/dist/chunk-6PKWQ3GH.js.map +0 -1
- package/dist/chunk-6XVTMLW4.cjs.map +0 -1
|
@@ -0,0 +1,305 @@
|
|
|
1
|
+
# libSQL Vector Store
|
|
2
|
+
|
|
3
|
+
The libSQL storage implementation provides a SQLite-compatible vector search [libSQL](https://github.com/tursodatabase/libsql), a fork of SQLite with vector extensions, and [Turso](https://turso.tech/) with vector extensions, offering a lightweight and efficient vector database solution. It's part of the `@mastra/libsql` package and offers efficient vector similarity search with metadata filtering.
|
|
4
|
+
|
|
5
|
+
## Installation
|
|
6
|
+
|
|
7
|
+
**npm**:
|
|
8
|
+
|
|
9
|
+
```bash
|
|
10
|
+
npm install @mastra/libsql@latest
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
**pnpm**:
|
|
14
|
+
|
|
15
|
+
```bash
|
|
16
|
+
pnpm add @mastra/libsql@latest
|
|
17
|
+
```
|
|
18
|
+
|
|
19
|
+
**Yarn**:
|
|
20
|
+
|
|
21
|
+
```bash
|
|
22
|
+
yarn add @mastra/libsql@latest
|
|
23
|
+
```
|
|
24
|
+
|
|
25
|
+
**Bun**:
|
|
26
|
+
|
|
27
|
+
```bash
|
|
28
|
+
bun add @mastra/libsql@latest
|
|
29
|
+
```
|
|
30
|
+
|
|
31
|
+
## Usage
|
|
32
|
+
|
|
33
|
+
```typescript
|
|
34
|
+
import { LibSQLVector } from "@mastra/libsql";
|
|
35
|
+
|
|
36
|
+
// Create a new vector store instance
|
|
37
|
+
const store = new LibSQLVector({
|
|
38
|
+
id: 'libsql-vector',
|
|
39
|
+
url: process.env.DATABASE_URL,
|
|
40
|
+
// Optional: for Turso cloud databases
|
|
41
|
+
authToken: process.env.DATABASE_AUTH_TOKEN,
|
|
42
|
+
});
|
|
43
|
+
|
|
44
|
+
// Create an index
|
|
45
|
+
await store.createIndex({
|
|
46
|
+
indexName: "myCollection",
|
|
47
|
+
dimension: 1536,
|
|
48
|
+
});
|
|
49
|
+
|
|
50
|
+
// Add vectors with metadata
|
|
51
|
+
const vectors = [[0.1, 0.2, ...], [0.3, 0.4, ...]];
|
|
52
|
+
const metadata = [
|
|
53
|
+
{ text: "first document", category: "A" },
|
|
54
|
+
{ text: "second document", category: "B" }
|
|
55
|
+
];
|
|
56
|
+
await store.upsert({
|
|
57
|
+
indexName: "myCollection",
|
|
58
|
+
vectors,
|
|
59
|
+
metadata,
|
|
60
|
+
});
|
|
61
|
+
|
|
62
|
+
// Query similar vectors
|
|
63
|
+
const queryVector = [0.1, 0.2, ...];
|
|
64
|
+
const results = await store.query({
|
|
65
|
+
indexName: "myCollection",
|
|
66
|
+
queryVector,
|
|
67
|
+
topK: 10, // top K results
|
|
68
|
+
filter: { category: "A" } // optional metadata filter
|
|
69
|
+
});
|
|
70
|
+
```
|
|
71
|
+
|
|
72
|
+
## Constructor Options
|
|
73
|
+
|
|
74
|
+
**url:** (`string`): libSQL database URL. Use ':memory:' for in-memory database, 'file:dbname.db' for local file, or a libSQL-compatible connection string like 'libsql://your-database.turso.io'.
|
|
75
|
+
|
|
76
|
+
**authToken?:** (`string`): Authentication token for Turso cloud databases
|
|
77
|
+
|
|
78
|
+
**syncUrl?:** (`string`): URL for database replication (Turso specific)
|
|
79
|
+
|
|
80
|
+
**syncInterval?:** (`number`): Interval in milliseconds for database sync (Turso specific)
|
|
81
|
+
|
|
82
|
+
## Methods
|
|
83
|
+
|
|
84
|
+
### createIndex()
|
|
85
|
+
|
|
86
|
+
Creates a new vector collection. The index name must start with a letter or underscore and can only contain letters, numbers, and underscores. The dimension must be a positive integer.
|
|
87
|
+
|
|
88
|
+
**indexName:** (`string`): Name of the index to create
|
|
89
|
+
|
|
90
|
+
**dimension:** (`number`): Vector dimension size (must match your embedding model)
|
|
91
|
+
|
|
92
|
+
**metric?:** (`'cosine' | 'euclidean' | 'dotproduct'`): Distance metric for similarity search. Note: Currently only cosine similarity is supported by libSQL. (Default: `cosine`)
|
|
93
|
+
|
|
94
|
+
### upsert()
|
|
95
|
+
|
|
96
|
+
Adds or updates vectors and their metadata in the index. Uses a transaction to ensure all vectors are inserted atomically - if any insert fails, the entire operation is rolled back.
|
|
97
|
+
|
|
98
|
+
**indexName:** (`string`): Name of the index to insert into
|
|
99
|
+
|
|
100
|
+
**vectors:** (`number[][]`): Array of embedding vectors
|
|
101
|
+
|
|
102
|
+
**metadata?:** (`Record<string, any>[]`): Metadata for each vector
|
|
103
|
+
|
|
104
|
+
**ids?:** (`string[]`): Optional vector IDs (auto-generated if not provided)
|
|
105
|
+
|
|
106
|
+
### query()
|
|
107
|
+
|
|
108
|
+
Searches for similar vectors with optional metadata filtering.
|
|
109
|
+
|
|
110
|
+
**indexName:** (`string`): Name of the index to search in
|
|
111
|
+
|
|
112
|
+
**queryVector:** (`number[]`): Query vector to find similar vectors for
|
|
113
|
+
|
|
114
|
+
**topK?:** (`number`): Number of results to return (Default: `10`)
|
|
115
|
+
|
|
116
|
+
**filter?:** (`Filter`): Metadata filters
|
|
117
|
+
|
|
118
|
+
**includeVector?:** (`boolean`): Whether to include vector data in results (Default: `false`)
|
|
119
|
+
|
|
120
|
+
**minScore?:** (`number`): Minimum similarity score threshold (Default: `0`)
|
|
121
|
+
|
|
122
|
+
### describeIndex()
|
|
123
|
+
|
|
124
|
+
Gets information about an index.
|
|
125
|
+
|
|
126
|
+
**indexName:** (`string`): Name of the index to describe
|
|
127
|
+
|
|
128
|
+
Returns:
|
|
129
|
+
|
|
130
|
+
```typescript
|
|
131
|
+
interface IndexStats {
|
|
132
|
+
dimension: number
|
|
133
|
+
count: number
|
|
134
|
+
metric: 'cosine' | 'euclidean' | 'dotproduct'
|
|
135
|
+
}
|
|
136
|
+
```
|
|
137
|
+
|
|
138
|
+
### deleteIndex()
|
|
139
|
+
|
|
140
|
+
Deletes an index and all its data.
|
|
141
|
+
|
|
142
|
+
**indexName:** (`string`): Name of the index to delete
|
|
143
|
+
|
|
144
|
+
### listIndexes()
|
|
145
|
+
|
|
146
|
+
Lists all vector indexes in the database.
|
|
147
|
+
|
|
148
|
+
Returns: `Promise<string[]>`
|
|
149
|
+
|
|
150
|
+
### truncateIndex()
|
|
151
|
+
|
|
152
|
+
Removes all vectors from an index while keeping the index structure.
|
|
153
|
+
|
|
154
|
+
**indexName:** (`string`): Name of the index to truncate
|
|
155
|
+
|
|
156
|
+
### updateVector()
|
|
157
|
+
|
|
158
|
+
Update a single vector by ID or by metadata filter. Either `id` or `filter` must be provided, but not both.
|
|
159
|
+
|
|
160
|
+
**indexName:** (`string`): Name of the index containing the vector
|
|
161
|
+
|
|
162
|
+
**id?:** (`string`): ID of the vector entry to update (mutually exclusive with filter)
|
|
163
|
+
|
|
164
|
+
**filter?:** (`Record<string, any>`): Metadata filter to identify vector(s) to update (mutually exclusive with id)
|
|
165
|
+
|
|
166
|
+
**update:** (`object`): Update data containing vector and/or metadata
|
|
167
|
+
|
|
168
|
+
**update.vector?:** (`number[]`): New vector data to update
|
|
169
|
+
|
|
170
|
+
**update.metadata?:** (`Record<string, any>`): New metadata to update
|
|
171
|
+
|
|
172
|
+
### deleteVector()
|
|
173
|
+
|
|
174
|
+
Deletes a specific vector entry from an index by its ID.
|
|
175
|
+
|
|
176
|
+
**indexName:** (`string`): Name of the index containing the vector
|
|
177
|
+
|
|
178
|
+
**id:** (`string`): ID of the vector entry to delete
|
|
179
|
+
|
|
180
|
+
### deleteVectors()
|
|
181
|
+
|
|
182
|
+
Delete multiple vectors by IDs or by metadata filter. Either `ids` or `filter` must be provided, but not both.
|
|
183
|
+
|
|
184
|
+
**indexName:** (`string`): Name of the index containing the vectors to delete
|
|
185
|
+
|
|
186
|
+
**ids?:** (`string[]`): Array of vector IDs to delete (mutually exclusive with filter)
|
|
187
|
+
|
|
188
|
+
**filter?:** (`Record<string, any>`): Metadata filter to identify vectors to delete (mutually exclusive with ids)
|
|
189
|
+
|
|
190
|
+
## Response Types
|
|
191
|
+
|
|
192
|
+
Query results are returned in this format:
|
|
193
|
+
|
|
194
|
+
```typescript
|
|
195
|
+
interface QueryResult {
|
|
196
|
+
id: string
|
|
197
|
+
score: number
|
|
198
|
+
metadata: Record<string, any>
|
|
199
|
+
vector?: number[] // Only included if includeVector is true
|
|
200
|
+
}
|
|
201
|
+
```
|
|
202
|
+
|
|
203
|
+
## Error Handling
|
|
204
|
+
|
|
205
|
+
The store throws specific errors for different failure cases:
|
|
206
|
+
|
|
207
|
+
```typescript
|
|
208
|
+
try {
|
|
209
|
+
await store.query({
|
|
210
|
+
indexName: 'my-collection',
|
|
211
|
+
queryVector: queryVector,
|
|
212
|
+
})
|
|
213
|
+
} catch (error) {
|
|
214
|
+
// Handle specific error cases
|
|
215
|
+
if (error.message.includes('Invalid index name format')) {
|
|
216
|
+
console.error(
|
|
217
|
+
'Index name must start with a letter/underscore and contain only alphanumeric characters',
|
|
218
|
+
)
|
|
219
|
+
} else if (error.message.includes('Table not found')) {
|
|
220
|
+
console.error('The specified index does not exist')
|
|
221
|
+
} else {
|
|
222
|
+
console.error('Vector store error:', error.message)
|
|
223
|
+
}
|
|
224
|
+
}
|
|
225
|
+
```
|
|
226
|
+
|
|
227
|
+
Common error cases include:
|
|
228
|
+
|
|
229
|
+
- Invalid index name format
|
|
230
|
+
- Invalid vector dimensions
|
|
231
|
+
- Table/index not found
|
|
232
|
+
- Database connection issues
|
|
233
|
+
- Transaction failures during upsert
|
|
234
|
+
|
|
235
|
+
## Usage Example
|
|
236
|
+
|
|
237
|
+
### Local embeddings with fastembed
|
|
238
|
+
|
|
239
|
+
Embeddings are numeric vectors used by memory's `semanticRecall` to retrieve related messages by meaning (not keywords). This setup uses `@mastra/fastembed` to generate vector embeddings.
|
|
240
|
+
|
|
241
|
+
Install `fastembed` to get started:
|
|
242
|
+
|
|
243
|
+
**npm**:
|
|
244
|
+
|
|
245
|
+
```bash
|
|
246
|
+
npm install @mastra/fastembed@latest
|
|
247
|
+
```
|
|
248
|
+
|
|
249
|
+
**pnpm**:
|
|
250
|
+
|
|
251
|
+
```bash
|
|
252
|
+
pnpm add @mastra/fastembed@latest
|
|
253
|
+
```
|
|
254
|
+
|
|
255
|
+
**Yarn**:
|
|
256
|
+
|
|
257
|
+
```bash
|
|
258
|
+
yarn add @mastra/fastembed@latest
|
|
259
|
+
```
|
|
260
|
+
|
|
261
|
+
**Bun**:
|
|
262
|
+
|
|
263
|
+
```bash
|
|
264
|
+
bun add @mastra/fastembed@latest
|
|
265
|
+
```
|
|
266
|
+
|
|
267
|
+
Add the following to your agent:
|
|
268
|
+
|
|
269
|
+
```typescript
|
|
270
|
+
import { Memory } from '@mastra/memory'
|
|
271
|
+
import { Agent } from '@mastra/core/agent'
|
|
272
|
+
import { LibSQLStore, LibSQLVector } from '@mastra/libsql'
|
|
273
|
+
import { fastembed } from '@mastra/fastembed'
|
|
274
|
+
|
|
275
|
+
export const libsqlAgent = new Agent({
|
|
276
|
+
id: 'libsql-agent',
|
|
277
|
+
name: 'libSQL Agent',
|
|
278
|
+
instructions:
|
|
279
|
+
'You are an AI agent with the ability to automatically recall memories from previous interactions.',
|
|
280
|
+
model: 'openai/gpt-5.1',
|
|
281
|
+
memory: new Memory({
|
|
282
|
+
storage: new LibSQLStore({
|
|
283
|
+
id: 'libsql-agent-storage',
|
|
284
|
+
url: 'file:libsql-agent.db',
|
|
285
|
+
}),
|
|
286
|
+
vector: new LibSQLVector({
|
|
287
|
+
id: 'libsql-agent-vector',
|
|
288
|
+
url: 'file:libsql-agent.db',
|
|
289
|
+
}),
|
|
290
|
+
embedder: fastembed,
|
|
291
|
+
options: {
|
|
292
|
+
lastMessages: 10,
|
|
293
|
+
semanticRecall: {
|
|
294
|
+
topK: 3,
|
|
295
|
+
messageRange: 2,
|
|
296
|
+
},
|
|
297
|
+
generateTitle: true, // Explicitly enable automatic title generation
|
|
298
|
+
},
|
|
299
|
+
}),
|
|
300
|
+
})
|
|
301
|
+
```
|
|
302
|
+
|
|
303
|
+
## Related
|
|
304
|
+
|
|
305
|
+
- [Metadata Filters](https://mastra.ai/reference/rag/metadata-filters)
|
|
@@ -0,0 +1,295 @@
|
|
|
1
|
+
# MongoDB Vector Store
|
|
2
|
+
|
|
3
|
+
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.
|
|
4
|
+
|
|
5
|
+
## Installation
|
|
6
|
+
|
|
7
|
+
**npm**:
|
|
8
|
+
|
|
9
|
+
```bash
|
|
10
|
+
npm install @mastra/mongodb@latest
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
**pnpm**:
|
|
14
|
+
|
|
15
|
+
```bash
|
|
16
|
+
pnpm add @mastra/mongodb@latest
|
|
17
|
+
```
|
|
18
|
+
|
|
19
|
+
**Yarn**:
|
|
20
|
+
|
|
21
|
+
```bash
|
|
22
|
+
yarn add @mastra/mongodb@latest
|
|
23
|
+
```
|
|
24
|
+
|
|
25
|
+
**Bun**:
|
|
26
|
+
|
|
27
|
+
```bash
|
|
28
|
+
bun add @mastra/mongodb@latest
|
|
29
|
+
```
|
|
30
|
+
|
|
31
|
+
## Usage Example
|
|
32
|
+
|
|
33
|
+
```typescript
|
|
34
|
+
import { MongoDBVector } from '@mastra/mongodb'
|
|
35
|
+
|
|
36
|
+
const store = new MongoDBVector({
|
|
37
|
+
id: 'mongodb-vector',
|
|
38
|
+
uri: process.env.MONGODB_URI,
|
|
39
|
+
dbName: process.env.MONGODB_DATABASE,
|
|
40
|
+
})
|
|
41
|
+
```
|
|
42
|
+
|
|
43
|
+
### Custom Embedding Field Path
|
|
44
|
+
|
|
45
|
+
If you need to store embeddings in a nested field structure (e.g., to integrate with existing MongoDB collections), use the `embeddingFieldPath` option:
|
|
46
|
+
|
|
47
|
+
```typescript
|
|
48
|
+
import { MongoDBVector } from '@mastra/mongodb'
|
|
49
|
+
|
|
50
|
+
const store = new MongoDBVector({
|
|
51
|
+
id: 'mongodb-vector',
|
|
52
|
+
uri: process.env.MONGODB_URI,
|
|
53
|
+
dbName: process.env.MONGODB_DATABASE,
|
|
54
|
+
embeddingFieldPath: 'text.contentEmbedding', // Store embeddings at text.contentEmbedding
|
|
55
|
+
})
|
|
56
|
+
```
|
|
57
|
+
|
|
58
|
+
## Constructor Options
|
|
59
|
+
|
|
60
|
+
**id:** (`string`): Unique identifier for this vector store instance
|
|
61
|
+
|
|
62
|
+
**uri:** (`string`): MongoDB connection string
|
|
63
|
+
|
|
64
|
+
**dbName:** (`string`): Name of the MongoDB database to use
|
|
65
|
+
|
|
66
|
+
**options?:** (`MongoClientOptions`): Optional MongoDB client options
|
|
67
|
+
|
|
68
|
+
**embeddingFieldPath?:** (`string`): Path to the field that stores vector embeddings. Supports nested paths using dot notation (e.g., 'text.contentEmbedding'). (Default: `embedding`)
|
|
69
|
+
|
|
70
|
+
## Methods
|
|
71
|
+
|
|
72
|
+
### createIndex()
|
|
73
|
+
|
|
74
|
+
Creates a new vector index (collection) in MongoDB.
|
|
75
|
+
|
|
76
|
+
**indexName:** (`string`): Name of the collection to create
|
|
77
|
+
|
|
78
|
+
**dimension:** (`number`): Vector dimension (must match your embedding model)
|
|
79
|
+
|
|
80
|
+
**metric?:** (`'cosine' | 'euclidean' | 'dotproduct'`): Distance metric for similarity search (Default: `cosine`)
|
|
81
|
+
|
|
82
|
+
### upsert()
|
|
83
|
+
|
|
84
|
+
Adds or updates vectors and their metadata in the collection.
|
|
85
|
+
|
|
86
|
+
**indexName:** (`string`): Name of the collection to insert into
|
|
87
|
+
|
|
88
|
+
**vectors:** (`number[][]`): Array of embedding vectors
|
|
89
|
+
|
|
90
|
+
**metadata?:** (`Record<string, any>[]`): Metadata for each vector
|
|
91
|
+
|
|
92
|
+
**ids?:** (`string[]`): Optional vector IDs (auto-generated if not provided)
|
|
93
|
+
|
|
94
|
+
### query()
|
|
95
|
+
|
|
96
|
+
Searches for similar vectors with optional metadata filtering.
|
|
97
|
+
|
|
98
|
+
**indexName:** (`string`): Name of the collection to search in
|
|
99
|
+
|
|
100
|
+
**queryVector:** (`number[]`): Query vector to find similar vectors for
|
|
101
|
+
|
|
102
|
+
**topK?:** (`number`): Number of results to return (Default: `10`)
|
|
103
|
+
|
|
104
|
+
**filter?:** (`Record<string, any>`): Metadata filters (applies to the \`metadata\` field)
|
|
105
|
+
|
|
106
|
+
**documentFilter?:** (`Record<string, any>`): Filters on original document fields (not just metadata)
|
|
107
|
+
|
|
108
|
+
**includeVector?:** (`boolean`): Whether to include vector data in results (Default: `false`)
|
|
109
|
+
|
|
110
|
+
**minScore?:** (`number`): Minimum similarity score threshold (Default: `0`)
|
|
111
|
+
|
|
112
|
+
### describeIndex()
|
|
113
|
+
|
|
114
|
+
Returns information about the index (collection).
|
|
115
|
+
|
|
116
|
+
**indexName:** (`string`): Name of the collection to describe
|
|
117
|
+
|
|
118
|
+
Returns:
|
|
119
|
+
|
|
120
|
+
```typescript
|
|
121
|
+
interface IndexStats {
|
|
122
|
+
dimension: number
|
|
123
|
+
count: number
|
|
124
|
+
metric: 'cosine' | 'euclidean' | 'dotproduct'
|
|
125
|
+
}
|
|
126
|
+
```
|
|
127
|
+
|
|
128
|
+
### deleteIndex()
|
|
129
|
+
|
|
130
|
+
Deletes a collection and all its data.
|
|
131
|
+
|
|
132
|
+
**indexName:** (`string`): Name of the collection to delete
|
|
133
|
+
|
|
134
|
+
### listIndexes()
|
|
135
|
+
|
|
136
|
+
Lists all vector collections in the MongoDB database.
|
|
137
|
+
|
|
138
|
+
Returns: `Promise<string[]>`
|
|
139
|
+
|
|
140
|
+
### updateVector()
|
|
141
|
+
|
|
142
|
+
Update a single vector by ID or by metadata filter. Either `id` or `filter` must be provided, but not both.
|
|
143
|
+
|
|
144
|
+
**indexName:** (`string`): Name of the collection containing the vector
|
|
145
|
+
|
|
146
|
+
**id?:** (`string`): ID of the vector entry to update (mutually exclusive with filter)
|
|
147
|
+
|
|
148
|
+
**filter?:** (`Record<string, any>`): Metadata filter to identify vector(s) to update (mutually exclusive with id)
|
|
149
|
+
|
|
150
|
+
**update:** (`object`): Update data containing vector and/or metadata
|
|
151
|
+
|
|
152
|
+
**update.vector?:** (`number[]`): New vector data to update
|
|
153
|
+
|
|
154
|
+
**update.metadata?:** (`Record<string, any>`): New metadata to update
|
|
155
|
+
|
|
156
|
+
### deleteVector()
|
|
157
|
+
|
|
158
|
+
Deletes a specific vector entry from an index by its ID.
|
|
159
|
+
|
|
160
|
+
**indexName:** (`string`): Name of the collection containing the vector
|
|
161
|
+
|
|
162
|
+
**id:** (`string`): ID of the vector entry to delete
|
|
163
|
+
|
|
164
|
+
### deleteVectors()
|
|
165
|
+
|
|
166
|
+
Delete multiple vectors by IDs or by metadata filter. Either `ids` or `filter` must be provided, but not both.
|
|
167
|
+
|
|
168
|
+
**indexName:** (`string`): Name of the collection containing the vectors to delete
|
|
169
|
+
|
|
170
|
+
**ids?:** (`string[]`): Array of vector IDs to delete (mutually exclusive with filter)
|
|
171
|
+
|
|
172
|
+
**filter?:** (`Record<string, any>`): Metadata filter to identify vectors to delete (mutually exclusive with ids)
|
|
173
|
+
|
|
174
|
+
### disconnect()
|
|
175
|
+
|
|
176
|
+
Closes the MongoDB client connection. Should be called when done using the store.
|
|
177
|
+
|
|
178
|
+
## Response Types
|
|
179
|
+
|
|
180
|
+
Query results are returned in this format:
|
|
181
|
+
|
|
182
|
+
```typescript
|
|
183
|
+
interface QueryResult {
|
|
184
|
+
id: string
|
|
185
|
+
score: number
|
|
186
|
+
metadata: Record<string, any>
|
|
187
|
+
vector?: number[] // Only included if includeVector is true
|
|
188
|
+
}
|
|
189
|
+
```
|
|
190
|
+
|
|
191
|
+
## Error Handling
|
|
192
|
+
|
|
193
|
+
The store throws typed errors that can be caught:
|
|
194
|
+
|
|
195
|
+
```typescript
|
|
196
|
+
try {
|
|
197
|
+
await store.query({
|
|
198
|
+
indexName: 'my_collection',
|
|
199
|
+
queryVector: queryVector,
|
|
200
|
+
})
|
|
201
|
+
} catch (error) {
|
|
202
|
+
// Handle specific error cases
|
|
203
|
+
if (error.message.includes('Invalid collection name')) {
|
|
204
|
+
console.error(
|
|
205
|
+
'Collection name must start with a letter or underscore and contain only valid characters.',
|
|
206
|
+
)
|
|
207
|
+
} else if (error.message.includes('Collection not found')) {
|
|
208
|
+
console.error('The specified collection does not exist')
|
|
209
|
+
} else {
|
|
210
|
+
console.error('Vector store error:', error.message)
|
|
211
|
+
}
|
|
212
|
+
}
|
|
213
|
+
```
|
|
214
|
+
|
|
215
|
+
## Best Practices
|
|
216
|
+
|
|
217
|
+
- Index metadata fields used in filters for optimal query performance.
|
|
218
|
+
- Use consistent field naming in metadata to avoid unexpected query results.
|
|
219
|
+
- Regularly monitor index and collection statistics to ensure efficient search.
|
|
220
|
+
|
|
221
|
+
## Usage Example
|
|
222
|
+
|
|
223
|
+
### Vector embeddings with MongoDB
|
|
224
|
+
|
|
225
|
+
Embeddings are numeric vectors used by memory's `semanticRecall` to retrieve related messages by meaning (not keywords).
|
|
226
|
+
|
|
227
|
+
> **Note:** You must use a deployment hosted on MongoDB Atlas to successfully use the MongoDB Vector database.
|
|
228
|
+
|
|
229
|
+
This setup uses FastEmbed, a local embedding model, to generate vector embeddings. To use this, install `@mastra/fastembed`:
|
|
230
|
+
|
|
231
|
+
**npm**:
|
|
232
|
+
|
|
233
|
+
```bash
|
|
234
|
+
npm install @mastra/fastembed@latest
|
|
235
|
+
```
|
|
236
|
+
|
|
237
|
+
**pnpm**:
|
|
238
|
+
|
|
239
|
+
```bash
|
|
240
|
+
pnpm add @mastra/fastembed@latest
|
|
241
|
+
```
|
|
242
|
+
|
|
243
|
+
**Yarn**:
|
|
244
|
+
|
|
245
|
+
```bash
|
|
246
|
+
yarn add @mastra/fastembed@latest
|
|
247
|
+
```
|
|
248
|
+
|
|
249
|
+
**Bun**:
|
|
250
|
+
|
|
251
|
+
```bash
|
|
252
|
+
bun add @mastra/fastembed@latest
|
|
253
|
+
```
|
|
254
|
+
|
|
255
|
+
Add the following to your agent:
|
|
256
|
+
|
|
257
|
+
```typescript
|
|
258
|
+
import { Memory } from '@mastra/memory'
|
|
259
|
+
import { Agent } from '@mastra/core/agent'
|
|
260
|
+
import { MongoDBStore, MongoDBVector } from '@mastra/mongodb'
|
|
261
|
+
import { fastembed } from '@mastra/fastembed'
|
|
262
|
+
|
|
263
|
+
export const mongodbAgent = new Agent({
|
|
264
|
+
id: 'mongodb-agent',
|
|
265
|
+
name: 'mongodb-agent',
|
|
266
|
+
instructions:
|
|
267
|
+
'You are an AI agent with the ability to automatically recall memories from previous interactions.',
|
|
268
|
+
model: 'openai/gpt-5.1',
|
|
269
|
+
memory: new Memory({
|
|
270
|
+
storage: new MongoDBStore({
|
|
271
|
+
id: 'mongodb-storage',
|
|
272
|
+
uri: process.env.MONGODB_URI!,
|
|
273
|
+
dbName: process.env.MONGODB_DB_NAME!,
|
|
274
|
+
}),
|
|
275
|
+
vector: new MongoDBVector({
|
|
276
|
+
id: 'mongodb-vector',
|
|
277
|
+
uri: process.env.MONGODB_URI!,
|
|
278
|
+
dbName: process.env.MONGODB_DB_NAME!,
|
|
279
|
+
}),
|
|
280
|
+
embedder: fastembed,
|
|
281
|
+
options: {
|
|
282
|
+
lastMessages: 10,
|
|
283
|
+
semanticRecall: {
|
|
284
|
+
topK: 3,
|
|
285
|
+
messageRange: 2,
|
|
286
|
+
},
|
|
287
|
+
generateTitle: true, // generates descriptive thread titles automatically
|
|
288
|
+
},
|
|
289
|
+
}),
|
|
290
|
+
})
|
|
291
|
+
```
|
|
292
|
+
|
|
293
|
+
## Related
|
|
294
|
+
|
|
295
|
+
- [Metadata Filters](https://mastra.ai/reference/rag/metadata-filters)
|