@mastra/mongodb 0.12.0 → 0.12.1
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/.turbo/turbo-build.log +7 -7
- package/CHANGELOG.md +57 -0
- package/LICENSE.md +11 -42
- package/dist/_tsup-dts-rollup.d.cts +376 -54
- package/dist/_tsup-dts-rollup.d.ts +376 -54
- package/dist/index.cjs +1420 -424
- package/dist/index.d.cts +0 -1
- package/dist/index.d.ts +0 -1
- package/dist/index.js +1414 -418
- package/docker-compose.yaml +1 -1
- package/package.json +6 -6
- package/src/storage/ConnectorHandler.ts +7 -0
- package/src/storage/MongoDBConnector.ts +93 -0
- package/src/storage/connectors/MongoDBConnector.ts +93 -0
- package/src/storage/connectors/base.ts +7 -0
- package/src/storage/domains/legacy-evals/index.ts +193 -0
- package/src/storage/domains/memory/index.ts +741 -0
- package/src/storage/domains/operations/index.ts +152 -0
- package/src/storage/domains/scores/index.ts +379 -0
- package/src/storage/domains/traces/index.ts +142 -0
- package/src/storage/domains/utils.ts +43 -0
- package/src/storage/domains/workflows/index.ts +196 -0
- package/src/storage/index.test.ts +24 -1226
- package/src/storage/index.ts +218 -776
- package/src/storage/types.ts +14 -0
- package/src/vector/index.test.ts +16 -1
- package/src/vector/index.ts +34 -11
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import type { MongoClientOptions } from 'mongodb';
|
|
2
|
+
import type { ConnectorHandler } from './connectors/base';
|
|
3
|
+
|
|
4
|
+
export type MongoDBConfig =
|
|
5
|
+
| DatabaseConfig
|
|
6
|
+
| {
|
|
7
|
+
connectorHandler: ConnectorHandler;
|
|
8
|
+
};
|
|
9
|
+
|
|
10
|
+
export type DatabaseConfig = {
|
|
11
|
+
url: string;
|
|
12
|
+
dbName: string;
|
|
13
|
+
options?: MongoClientOptions;
|
|
14
|
+
};
|
package/src/vector/index.test.ts
CHANGED
|
@@ -263,7 +263,10 @@ describe('MongoDBVector Integration Tests', () => {
|
|
|
263
263
|
beforeAll(async () => {
|
|
264
264
|
// Ensure testIndexName2 has at least one document
|
|
265
265
|
const testVector = [1, 0, 0, 0];
|
|
266
|
-
const testMetadata = {
|
|
266
|
+
const testMetadata = {
|
|
267
|
+
label: 'test_filter_validation',
|
|
268
|
+
timestamp: new Date('2024-01-01T00:00:00Z'),
|
|
269
|
+
};
|
|
267
270
|
|
|
268
271
|
// First check if there are already documents
|
|
269
272
|
const existingResults = await vectorDB.query({
|
|
@@ -338,6 +341,18 @@ describe('MongoDBVector Integration Tests', () => {
|
|
|
338
341
|
expect(results.length).toBeGreaterThan(0);
|
|
339
342
|
});
|
|
340
343
|
|
|
344
|
+
it('handles filters with multiple properties', async () => {
|
|
345
|
+
const results = await retryQuery({
|
|
346
|
+
indexName: testIndexName2,
|
|
347
|
+
queryVector: [1, 0, 0, 0],
|
|
348
|
+
filter: {
|
|
349
|
+
'metadata.label': 'test_filter_validation',
|
|
350
|
+
'metadata.timestamp': { $gt: new Date('2023-01-01T00:00:00Z') },
|
|
351
|
+
},
|
|
352
|
+
});
|
|
353
|
+
expect(results.length).toBeGreaterThan(0);
|
|
354
|
+
});
|
|
355
|
+
|
|
341
356
|
it('normalizes date values in filter using filter.ts', async () => {
|
|
342
357
|
const vector = [1, 0, 0, 0];
|
|
343
358
|
const timestampDate = new Date('2024-01-01T00:00:00Z');
|
package/src/vector/index.ts
CHANGED
|
@@ -135,8 +135,8 @@ export class MongoDBVector extends MastraVector<MongoDBVectorFilter> {
|
|
|
135
135
|
const embeddingField = this.embeddingFieldName;
|
|
136
136
|
const numDimensions = dimension;
|
|
137
137
|
|
|
138
|
-
// Create
|
|
139
|
-
await
|
|
138
|
+
// Create search indexes
|
|
139
|
+
await collection.createSearchIndex({
|
|
140
140
|
definition: {
|
|
141
141
|
fields: [
|
|
142
142
|
{
|
|
@@ -145,11 +145,24 @@ export class MongoDBVector extends MastraVector<MongoDBVectorFilter> {
|
|
|
145
145
|
numDimensions: numDimensions,
|
|
146
146
|
similarity: mongoMetric,
|
|
147
147
|
},
|
|
148
|
+
{
|
|
149
|
+
type: 'filter',
|
|
150
|
+
path: '_id',
|
|
151
|
+
},
|
|
148
152
|
],
|
|
149
153
|
},
|
|
150
154
|
name: indexNameInternal,
|
|
151
155
|
type: 'vectorSearch',
|
|
152
156
|
});
|
|
157
|
+
await collection.createSearchIndex({
|
|
158
|
+
definition: {
|
|
159
|
+
mappings: {
|
|
160
|
+
dynamic: true,
|
|
161
|
+
},
|
|
162
|
+
},
|
|
163
|
+
name: `${indexName}_search_index`,
|
|
164
|
+
type: 'search',
|
|
165
|
+
});
|
|
153
166
|
} catch (error: any) {
|
|
154
167
|
if (error.codeName !== 'IndexAlreadyExists') {
|
|
155
168
|
throw new MastraError(
|
|
@@ -299,19 +312,29 @@ export class MongoDBVector extends MastraVector<MongoDBVectorFilter> {
|
|
|
299
312
|
combinedFilter = documentMongoFilter;
|
|
300
313
|
}
|
|
301
314
|
|
|
315
|
+
const vectorSearch: Document = {
|
|
316
|
+
index: indexNameInternal,
|
|
317
|
+
queryVector: queryVector,
|
|
318
|
+
path: this.embeddingFieldName,
|
|
319
|
+
numCandidates: 100,
|
|
320
|
+
limit: topK,
|
|
321
|
+
};
|
|
322
|
+
|
|
323
|
+
if (Object.keys(combinedFilter).length > 0) {
|
|
324
|
+
// pre-filter for candidate document IDs
|
|
325
|
+
const candidateIds = await collection
|
|
326
|
+
.aggregate([{ $match: combinedFilter }, { $project: { _id: 1 } }])
|
|
327
|
+
.map(doc => doc._id)
|
|
328
|
+
.toArray();
|
|
329
|
+
|
|
330
|
+
vectorSearch.filter = { _id: { $in: candidateIds } };
|
|
331
|
+
}
|
|
332
|
+
|
|
302
333
|
// Build the aggregation pipeline
|
|
303
334
|
const pipeline = [
|
|
304
335
|
{
|
|
305
|
-
$vectorSearch:
|
|
306
|
-
index: indexNameInternal,
|
|
307
|
-
queryVector: queryVector,
|
|
308
|
-
path: this.embeddingFieldName,
|
|
309
|
-
numCandidates: 100,
|
|
310
|
-
limit: topK,
|
|
311
|
-
},
|
|
336
|
+
$vectorSearch: vectorSearch,
|
|
312
337
|
},
|
|
313
|
-
// Apply the filter using $match stage
|
|
314
|
-
...(Object.keys(combinedFilter).length > 0 ? [{ $match: combinedFilter }] : []),
|
|
315
338
|
{
|
|
316
339
|
$set: { score: { $meta: 'vectorSearchScore' } },
|
|
317
340
|
},
|