@mastra/mongodb 1.0.0-beta.9 → 1.1.0-alpha.0
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 +1095 -0
- package/README.md +18 -0
- package/dist/docs/README.md +34 -0
- package/dist/docs/SKILL.md +35 -0
- package/dist/docs/SOURCE_MAP.json +6 -0
- package/dist/docs/memory/01-working-memory.md +411 -0
- package/dist/docs/rag/01-vector-databases.md +643 -0
- package/dist/docs/rag/02-retrieval.md +548 -0
- package/dist/docs/storage/01-reference.md +243 -0
- package/dist/docs/vectors/01-reference.md +201 -0
- package/dist/index.cjs +526 -52
- package/dist/index.cjs.map +1 -1
- package/dist/index.js +528 -54
- package/dist/index.js.map +1 -1
- package/dist/storage/db/index.d.ts.map +1 -1
- package/dist/storage/domains/agents/index.d.ts +11 -1
- package/dist/storage/domains/agents/index.d.ts.map +1 -1
- package/dist/storage/domains/memory/index.d.ts +2 -2
- package/dist/storage/domains/memory/index.d.ts.map +1 -1
- package/dist/storage/domains/observability/index.d.ts +48 -5
- package/dist/storage/domains/observability/index.d.ts.map +1 -1
- package/dist/storage/index.d.ts +2 -2
- package/dist/storage/index.d.ts.map +1 -1
- package/dist/storage/types.d.ts +14 -2
- package/dist/storage/types.d.ts.map +1 -1
- package/dist/vector/index.d.ts +17 -6
- package/dist/vector/index.d.ts.map +1 -1
- package/package.json +14 -13
|
@@ -0,0 +1,243 @@
|
|
|
1
|
+
# Storage API Reference
|
|
2
|
+
|
|
3
|
+
> API reference for storage - 1 entries
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
---
|
|
7
|
+
|
|
8
|
+
## Reference: MongoDB Storage
|
|
9
|
+
|
|
10
|
+
> Documentation for the MongoDB storage implementation in Mastra.
|
|
11
|
+
|
|
12
|
+
The MongoDB storage implementation provides a scalable storage solution using MongoDB databases with support for both document storage and vector operations.
|
|
13
|
+
|
|
14
|
+
## Installation
|
|
15
|
+
|
|
16
|
+
```bash npm2yarn
|
|
17
|
+
npm install @mastra/mongodb@latest
|
|
18
|
+
```
|
|
19
|
+
|
|
20
|
+
## Usage
|
|
21
|
+
|
|
22
|
+
Ensure you have a MongoDB Atlas Local (via Docker) or MongoDB Atlas Cloud instance with Atlas Search enabled. MongoDB 7.0+ is recommended.
|
|
23
|
+
|
|
24
|
+
```typescript
|
|
25
|
+
import { MongoDBStore } from "@mastra/mongodb";
|
|
26
|
+
|
|
27
|
+
const storage = new MongoDBStore({
|
|
28
|
+
id: 'mongodb-storage',
|
|
29
|
+
uri: process.env.MONGODB_URI,
|
|
30
|
+
dbName: process.env.MONGODB_DATABASE,
|
|
31
|
+
});
|
|
32
|
+
```
|
|
33
|
+
|
|
34
|
+
## Parameters
|
|
35
|
+
|
|
36
|
+
> **Deprecation Notice**
|
|
37
|
+
The `url` parameter is deprecated but still supported for backward compatibility. Please use `uri` instead in all new code.
|
|
38
|
+
|
|
39
|
+
## Constructor Examples
|
|
40
|
+
|
|
41
|
+
You can instantiate `MongoDBStore` in the following ways:
|
|
42
|
+
|
|
43
|
+
```ts
|
|
44
|
+
import { MongoDBStore } from "@mastra/mongodb";
|
|
45
|
+
|
|
46
|
+
// Basic connection without custom options
|
|
47
|
+
const store1 = new MongoDBStore({
|
|
48
|
+
id: 'mongodb-storage-01',
|
|
49
|
+
uri: "mongodb+srv://user:password@cluster.mongodb.net",
|
|
50
|
+
dbName: "mastra_storage",
|
|
51
|
+
});
|
|
52
|
+
|
|
53
|
+
// Using connection string with options
|
|
54
|
+
const store2 = new MongoDBStore({
|
|
55
|
+
id: 'mongodb-storage-02',
|
|
56
|
+
uri: "mongodb+srv://user:password@cluster.mongodb.net",
|
|
57
|
+
dbName: "mastra_storage",
|
|
58
|
+
options: {
|
|
59
|
+
retryWrites: true,
|
|
60
|
+
maxPoolSize: 10,
|
|
61
|
+
serverSelectionTimeoutMS: 5000,
|
|
62
|
+
socketTimeoutMS: 45000,
|
|
63
|
+
},
|
|
64
|
+
});
|
|
65
|
+
```
|
|
66
|
+
|
|
67
|
+
## Additional Notes
|
|
68
|
+
|
|
69
|
+
### Collection Management
|
|
70
|
+
|
|
71
|
+
The storage implementation handles collection creation and management automatically. It creates the following collections:
|
|
72
|
+
|
|
73
|
+
- `mastra_workflow_snapshot`: Stores workflow state and execution data
|
|
74
|
+
- `mastra_evals`: Stores evaluation results and metadata
|
|
75
|
+
- `mastra_threads`: Stores conversation threads
|
|
76
|
+
- `mastra_messages`: Stores individual messages
|
|
77
|
+
- `mastra_traces`: Stores telemetry and tracing data
|
|
78
|
+
- `mastra_scorers`: Stores scoring and evaluation data
|
|
79
|
+
- `mastra_resources`: Stores resource working memory data
|
|
80
|
+
|
|
81
|
+
### Initialization
|
|
82
|
+
|
|
83
|
+
When you pass storage to the Mastra class, `init()` is called automatically before any storage operation:
|
|
84
|
+
|
|
85
|
+
```typescript
|
|
86
|
+
import { Mastra } from "@mastra/core";
|
|
87
|
+
import { MongoDBStore } from "@mastra/mongodb";
|
|
88
|
+
|
|
89
|
+
const storage = new MongoDBStore({
|
|
90
|
+
id: 'mongodb-storage',
|
|
91
|
+
uri: process.env.MONGODB_URI,
|
|
92
|
+
dbName: process.env.MONGODB_DATABASE,
|
|
93
|
+
});
|
|
94
|
+
|
|
95
|
+
const mastra = new Mastra({
|
|
96
|
+
storage, // init() is called automatically
|
|
97
|
+
});
|
|
98
|
+
```
|
|
99
|
+
|
|
100
|
+
If you're using storage directly without Mastra, you must call `init()` explicitly to create the collections:
|
|
101
|
+
|
|
102
|
+
```typescript
|
|
103
|
+
import { MongoDBStore } from "@mastra/mongodb";
|
|
104
|
+
|
|
105
|
+
const storage = new MongoDBStore({
|
|
106
|
+
id: 'mongodb-storage',
|
|
107
|
+
uri: process.env.MONGODB_URI,
|
|
108
|
+
dbName: process.env.MONGODB_DATABASE,
|
|
109
|
+
});
|
|
110
|
+
|
|
111
|
+
// Required when using storage directly
|
|
112
|
+
await storage.init();
|
|
113
|
+
|
|
114
|
+
// Access domain-specific stores via getStore()
|
|
115
|
+
const memoryStore = await storage.getStore('memory');
|
|
116
|
+
const thread = await memoryStore?.getThreadById({ threadId: "..." });
|
|
117
|
+
```
|
|
118
|
+
|
|
119
|
+
> **Note:**
|
|
120
|
+
If `init()` is not called, collections won't be created and storage operations will fail silently or throw errors.
|
|
121
|
+
|
|
122
|
+
## Vector Search Capabilities
|
|
123
|
+
|
|
124
|
+
MongoDB storage includes built-in vector search capabilities for AI applications:
|
|
125
|
+
|
|
126
|
+
### Vector Index Creation
|
|
127
|
+
|
|
128
|
+
```typescript
|
|
129
|
+
import { MongoDBVector } from "@mastra/mongodb";
|
|
130
|
+
|
|
131
|
+
const vectorStore = new MongoDBVector({
|
|
132
|
+
id: 'mongodb-vector',
|
|
133
|
+
uri: process.env.MONGODB_URI,
|
|
134
|
+
dbName: process.env.MONGODB_DATABASE,
|
|
135
|
+
});
|
|
136
|
+
|
|
137
|
+
// Create a vector index for embeddings
|
|
138
|
+
await vectorStore.createIndex({
|
|
139
|
+
indexName: "document_embeddings",
|
|
140
|
+
dimension: 1536,
|
|
141
|
+
});
|
|
142
|
+
```
|
|
143
|
+
|
|
144
|
+
### Vector Operations
|
|
145
|
+
|
|
146
|
+
```typescript
|
|
147
|
+
// Store vectors with metadata
|
|
148
|
+
await vectorStore.upsert({
|
|
149
|
+
indexName: "document_embeddings",
|
|
150
|
+
vectors: [
|
|
151
|
+
{
|
|
152
|
+
id: "doc-1",
|
|
153
|
+
values: [0.1, 0.2, 0.3, ...], // 1536-dimensional vector
|
|
154
|
+
metadata: {
|
|
155
|
+
title: "Document Title",
|
|
156
|
+
category: "technical",
|
|
157
|
+
source: "api-docs",
|
|
158
|
+
},
|
|
159
|
+
},
|
|
160
|
+
],
|
|
161
|
+
});
|
|
162
|
+
|
|
163
|
+
// Similarity search
|
|
164
|
+
const results = await vectorStore.query({
|
|
165
|
+
indexName: "document_embeddings",
|
|
166
|
+
vector: queryEmbedding,
|
|
167
|
+
topK: 5,
|
|
168
|
+
filter: {
|
|
169
|
+
category: "technical",
|
|
170
|
+
},
|
|
171
|
+
});
|
|
172
|
+
```
|
|
173
|
+
|
|
174
|
+
## Usage Example
|
|
175
|
+
|
|
176
|
+
### Adding memory to an agent
|
|
177
|
+
|
|
178
|
+
To add MongoDB memory to an agent use the `Memory` class and create a new `storage` key using `MongoDBStore`. The configuration supports both local and remote MongoDB instances.
|
|
179
|
+
|
|
180
|
+
```typescript title="src/mastra/agents/example-mongodb-agent.ts"
|
|
181
|
+
import { Memory } from "@mastra/memory";
|
|
182
|
+
import { Agent } from "@mastra/core/agent";
|
|
183
|
+
import { MongoDBStore } from "@mastra/mongodb";
|
|
184
|
+
|
|
185
|
+
export const mongodbAgent = new Agent({
|
|
186
|
+
id: "mongodb-agent",
|
|
187
|
+
name: "mongodb-agent",
|
|
188
|
+
instructions:
|
|
189
|
+
"You are an AI agent with the ability to automatically recall memories from previous interactions.",
|
|
190
|
+
model: "openai/gpt-5.1",
|
|
191
|
+
memory: new Memory({
|
|
192
|
+
storage: new MongoDBStore({
|
|
193
|
+
uri: process.env.MONGODB_URI!,
|
|
194
|
+
dbName: process.env.MONGODB_DB_NAME!,
|
|
195
|
+
}),
|
|
196
|
+
options: {
|
|
197
|
+
generateTitle: true,
|
|
198
|
+
},
|
|
199
|
+
}),
|
|
200
|
+
});
|
|
201
|
+
```
|
|
202
|
+
|
|
203
|
+
### Using the agent
|
|
204
|
+
|
|
205
|
+
Use `memoryOptions` to scope recall for this request. Set `lastMessages: 5` to limit recency-based recall, and use `semanticRecall` to fetch the `topK: 3` most relevant messages, including `messageRange: 2` neighboring messages for context around each match.
|
|
206
|
+
|
|
207
|
+
```typescript title="src/test-mongodb-agent.ts"
|
|
208
|
+
import "dotenv/config";
|
|
209
|
+
|
|
210
|
+
import { mastra } from "./mastra";
|
|
211
|
+
|
|
212
|
+
const threadId = "123";
|
|
213
|
+
const resourceId = "user-456";
|
|
214
|
+
|
|
215
|
+
const agent = mastra.getAgent("mongodbAgent");
|
|
216
|
+
|
|
217
|
+
const message = await agent.stream("My name is Mastra", {
|
|
218
|
+
memory: {
|
|
219
|
+
thread: threadId,
|
|
220
|
+
resource: resourceId,
|
|
221
|
+
},
|
|
222
|
+
});
|
|
223
|
+
|
|
224
|
+
await message.textStream.pipeTo(new WritableStream());
|
|
225
|
+
|
|
226
|
+
const stream = await agent.stream("What's my name?", {
|
|
227
|
+
memory: {
|
|
228
|
+
thread: threadId,
|
|
229
|
+
resource: resourceId,
|
|
230
|
+
},
|
|
231
|
+
memoryOptions: {
|
|
232
|
+
lastMessages: 5,
|
|
233
|
+
semanticRecall: {
|
|
234
|
+
topK: 3,
|
|
235
|
+
messageRange: 2,
|
|
236
|
+
},
|
|
237
|
+
},
|
|
238
|
+
});
|
|
239
|
+
|
|
240
|
+
for await (const chunk of stream.textStream) {
|
|
241
|
+
process.stdout.write(chunk);
|
|
242
|
+
}
|
|
243
|
+
```
|
|
@@ -0,0 +1,201 @@
|
|
|
1
|
+
# Vectors API Reference
|
|
2
|
+
|
|
3
|
+
> API reference for vectors - 1 entries
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
---
|
|
7
|
+
|
|
8
|
+
## Reference: MongoDB Vector Store
|
|
9
|
+
|
|
10
|
+
> Documentation for the MongoDBVector class in Mastra, which provides vector search using MongoDB Atlas and Atlas Vector Search.
|
|
11
|
+
|
|
12
|
+
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.
|
|
13
|
+
|
|
14
|
+
## Installation
|
|
15
|
+
|
|
16
|
+
```bash npm2yarn
|
|
17
|
+
npm install @mastra/mongodb@latest
|
|
18
|
+
```
|
|
19
|
+
|
|
20
|
+
## Usage Example
|
|
21
|
+
|
|
22
|
+
```typescript
|
|
23
|
+
import { MongoDBVector } from "@mastra/mongodb";
|
|
24
|
+
|
|
25
|
+
const store = new MongoDBVector({
|
|
26
|
+
id: 'mongodb-vector',
|
|
27
|
+
uri: process.env.MONGODB_URI,
|
|
28
|
+
dbName: process.env.MONGODB_DATABASE,
|
|
29
|
+
});
|
|
30
|
+
```
|
|
31
|
+
|
|
32
|
+
### Custom Embedding Field Path
|
|
33
|
+
|
|
34
|
+
If you need to store embeddings in a nested field structure (e.g., to integrate with existing MongoDB collections), use the `embeddingFieldPath` option:
|
|
35
|
+
|
|
36
|
+
```typescript
|
|
37
|
+
import { MongoDBVector } from "@mastra/mongodb";
|
|
38
|
+
|
|
39
|
+
const store = new MongoDBVector({
|
|
40
|
+
id: 'mongodb-vector',
|
|
41
|
+
uri: process.env.MONGODB_URI,
|
|
42
|
+
dbName: process.env.MONGODB_DATABASE,
|
|
43
|
+
embeddingFieldPath: 'text.contentEmbedding', // Store embeddings at text.contentEmbedding
|
|
44
|
+
});
|
|
45
|
+
```
|
|
46
|
+
|
|
47
|
+
## Constructor Options
|
|
48
|
+
|
|
49
|
+
## Methods
|
|
50
|
+
|
|
51
|
+
### createIndex()
|
|
52
|
+
|
|
53
|
+
Creates a new vector index (collection) in MongoDB.
|
|
54
|
+
|
|
55
|
+
### upsert()
|
|
56
|
+
|
|
57
|
+
Adds or updates vectors and their metadata in the collection.
|
|
58
|
+
|
|
59
|
+
### query()
|
|
60
|
+
|
|
61
|
+
Searches for similar vectors with optional metadata filtering.
|
|
62
|
+
|
|
63
|
+
### describeIndex()
|
|
64
|
+
|
|
65
|
+
Returns information about the index (collection).
|
|
66
|
+
|
|
67
|
+
Returns:
|
|
68
|
+
|
|
69
|
+
```typescript
|
|
70
|
+
interface IndexStats {
|
|
71
|
+
dimension: number;
|
|
72
|
+
count: number;
|
|
73
|
+
metric: "cosine" | "euclidean" | "dotproduct";
|
|
74
|
+
}
|
|
75
|
+
```
|
|
76
|
+
|
|
77
|
+
### deleteIndex()
|
|
78
|
+
|
|
79
|
+
Deletes a collection and all its data.
|
|
80
|
+
|
|
81
|
+
### listIndexes()
|
|
82
|
+
|
|
83
|
+
Lists all vector collections in the MongoDB database.
|
|
84
|
+
|
|
85
|
+
Returns: `Promise<string[]>`
|
|
86
|
+
|
|
87
|
+
### updateVector()
|
|
88
|
+
|
|
89
|
+
Update a single vector by ID or by metadata filter. Either `id` or `filter` must be provided, but not both.
|
|
90
|
+
|
|
91
|
+
### deleteVector()
|
|
92
|
+
|
|
93
|
+
Deletes a specific vector entry from an index by its ID.
|
|
94
|
+
|
|
95
|
+
### deleteVectors()
|
|
96
|
+
|
|
97
|
+
Delete multiple vectors by IDs or by metadata filter. Either `ids` or `filter` must be provided, but not both.
|
|
98
|
+
|
|
99
|
+
### disconnect()
|
|
100
|
+
|
|
101
|
+
Closes the MongoDB client connection. Should be called when done using the store.
|
|
102
|
+
|
|
103
|
+
## Response Types
|
|
104
|
+
|
|
105
|
+
Query results are returned in this format:
|
|
106
|
+
|
|
107
|
+
```typescript
|
|
108
|
+
interface QueryResult {
|
|
109
|
+
id: string;
|
|
110
|
+
score: number;
|
|
111
|
+
metadata: Record<string, any>;
|
|
112
|
+
vector?: number[]; // Only included if includeVector is true
|
|
113
|
+
}
|
|
114
|
+
```
|
|
115
|
+
|
|
116
|
+
## Error Handling
|
|
117
|
+
|
|
118
|
+
The store throws typed errors that can be caught:
|
|
119
|
+
|
|
120
|
+
```typescript
|
|
121
|
+
try {
|
|
122
|
+
await store.query({
|
|
123
|
+
indexName: "my_collection",
|
|
124
|
+
queryVector: queryVector,
|
|
125
|
+
});
|
|
126
|
+
} catch (error) {
|
|
127
|
+
// Handle specific error cases
|
|
128
|
+
if (error.message.includes("Invalid collection name")) {
|
|
129
|
+
console.error(
|
|
130
|
+
"Collection name must start with a letter or underscore and contain only valid characters.",
|
|
131
|
+
);
|
|
132
|
+
} else if (error.message.includes("Collection not found")) {
|
|
133
|
+
console.error("The specified collection does not exist");
|
|
134
|
+
} else {
|
|
135
|
+
console.error("Vector store error:", error.message);
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
```
|
|
139
|
+
|
|
140
|
+
## Best Practices
|
|
141
|
+
|
|
142
|
+
- Index metadata fields used in filters for optimal query performance.
|
|
143
|
+
- Use consistent field naming in metadata to avoid unexpected query results.
|
|
144
|
+
- Regularly monitor index and collection statistics to ensure efficient search.
|
|
145
|
+
|
|
146
|
+
## Usage Example
|
|
147
|
+
|
|
148
|
+
### Vector embeddings with MongoDB
|
|
149
|
+
|
|
150
|
+
Embeddings are numeric vectors used by memory's `semanticRecall` to retrieve related messages by meaning (not keywords).
|
|
151
|
+
|
|
152
|
+
> Note: You must use a deployment hosted on MongoDB Atlas to successfully use the MongoDB Vector database.
|
|
153
|
+
|
|
154
|
+
This setup uses FastEmbed, a local embedding model, to generate vector embeddings.
|
|
155
|
+
To use this, install `@mastra/fastembed`:
|
|
156
|
+
|
|
157
|
+
```bash npm2yarn
|
|
158
|
+
npm install @mastra/fastembed@latest
|
|
159
|
+
```
|
|
160
|
+
|
|
161
|
+
Add the following to your agent:
|
|
162
|
+
|
|
163
|
+
```typescript title="src/mastra/agents/example-mongodb-agent.ts"
|
|
164
|
+
import { Memory } from "@mastra/memory";
|
|
165
|
+
import { Agent } from "@mastra/core/agent";
|
|
166
|
+
import { MongoDBStore, MongoDBVector } from "@mastra/mongodb";
|
|
167
|
+
import { fastembed } from "@mastra/fastembed";
|
|
168
|
+
|
|
169
|
+
export const mongodbAgent = new Agent({
|
|
170
|
+
id: "mongodb-agent",
|
|
171
|
+
name: "mongodb-agent",
|
|
172
|
+
instructions:
|
|
173
|
+
"You are an AI agent with the ability to automatically recall memories from previous interactions.",
|
|
174
|
+
model: "openai/gpt-5.1",
|
|
175
|
+
memory: new Memory({
|
|
176
|
+
storage: new MongoDBStore({
|
|
177
|
+
id: 'mongodb-storage',
|
|
178
|
+
uri: process.env.MONGODB_URI!,
|
|
179
|
+
dbName: process.env.MONGODB_DB_NAME!,
|
|
180
|
+
}),
|
|
181
|
+
vector: new MongoDBVector({
|
|
182
|
+
id: 'mongodb-vector',
|
|
183
|
+
uri: process.env.MONGODB_URI!,
|
|
184
|
+
dbName: process.env.MONGODB_DB_NAME!,
|
|
185
|
+
}),
|
|
186
|
+
embedder: fastembed,
|
|
187
|
+
options: {
|
|
188
|
+
lastMessages: 10,
|
|
189
|
+
semanticRecall: {
|
|
190
|
+
topK: 3,
|
|
191
|
+
messageRange: 2,
|
|
192
|
+
},
|
|
193
|
+
generateTitle: true, // generates descriptive thread titles automatically
|
|
194
|
+
},
|
|
195
|
+
}),
|
|
196
|
+
});
|
|
197
|
+
```
|
|
198
|
+
|
|
199
|
+
## Related
|
|
200
|
+
|
|
201
|
+
- [Metadata Filters](../rag/metadata-filters)
|