@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.
@@ -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
+ };
@@ -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 = { label: 'test_filter_validation' };
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');
@@ -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 the search index
139
- await (collection as any).createSearchIndex({
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
  },