@mastra/mongodb 0.0.2-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.
@@ -0,0 +1,23 @@
1
+
2
+ > @mastra/mongodb@0.0.2-alpha.0 build /home/runner/work/mastra/mastra/stores/mongodb
3
+ > tsup src/index.ts --format esm,cjs --experimental-dts --clean --treeshake=smallest --splitting
4
+
5
+ CLI Building entry: src/index.ts
6
+ CLI Using tsconfig: tsconfig.json
7
+ CLI tsup v8.4.0
8
+ TSC Build start
9
+ TSC ⚡️ Build success in 7439ms
10
+ DTS Build start
11
+ CLI Target: es2022
12
+ Analysis will use the bundled TypeScript version 5.8.2
13
+ Writing package typings: /home/runner/work/mastra/mastra/stores/mongodb/dist/_tsup-dts-rollup.d.ts
14
+ Analysis will use the bundled TypeScript version 5.8.2
15
+ Writing package typings: /home/runner/work/mastra/mastra/stores/mongodb/dist/_tsup-dts-rollup.d.cts
16
+ DTS ⚡️ Build success in 9094ms
17
+ CLI Cleaning output folder
18
+ ESM Build start
19
+ CJS Build start
20
+ CJS dist/index.cjs 12.43 KB
21
+ CJS ⚡️ Build success in 784ms
22
+ ESM dist/index.js 12.39 KB
23
+ ESM ⚡️ Build success in 784ms
package/CHANGELOG.md ADDED
@@ -0,0 +1,15 @@
1
+ # @mastra/mongodb
2
+
3
+ ## 0.0.2-alpha.0
4
+
5
+ ### Patch Changes
6
+
7
+ - c18f562: MongoDBVector for Vector Database support
8
+ - Updated dependencies [20275d4]
9
+ - Updated dependencies [7d1892c]
10
+ - Updated dependencies [a90a082]
11
+ - Updated dependencies [35955b0]
12
+ - Updated dependencies [c1409ef]
13
+ - Updated dependencies [11d4485]
14
+ - Updated dependencies [2d4001d]
15
+ - @mastra/core@0.9.1-alpha.1
package/LICENSE.md ADDED
@@ -0,0 +1,46 @@
1
+ # Elastic License 2.0 (ELv2)
2
+
3
+ Copyright (c) 2025 Mastra AI, Inc.
4
+
5
+ **Acceptance**
6
+ By using the software, you agree to all of the terms and conditions below.
7
+
8
+ **Copyright License**
9
+ The licensor grants you a non-exclusive, royalty-free, worldwide, non-sublicensable, non-transferable license to use, copy, distribute, make available, and prepare derivative works of the software, in each case subject to the limitations and conditions below
10
+
11
+ **Limitations**
12
+ You may not provide the software to third parties as a hosted or managed service, where the service provides users with access to any substantial set of the features or functionality of the software.
13
+
14
+ You may not move, change, disable, or circumvent the license key functionality in the software, and you may not remove or obscure any functionality in the software that is protected by the license key.
15
+
16
+ You may not alter, remove, or obscure any licensing, copyright, or other notices of the licensor in the software. Any use of the licensor’s trademarks is subject to applicable law.
17
+
18
+ **Patents**
19
+ The licensor grants you a license, under any patent claims the licensor can license, or becomes able to license, to make, have made, use, sell, offer for sale, import and have imported the software, in each case subject to the limitations and conditions in this license. This license does not cover any patent claims that you cause to be infringed by modifications or additions to the software. If you or your company make any written claim that the software infringes or contributes to infringement of any patent, your patent license for the software granted under these terms ends immediately. If your company makes such a claim, your patent license ends immediately for work on behalf of your company.
20
+
21
+ **Notices**
22
+ You must ensure that anyone who gets a copy of any part of the software from you also gets a copy of these terms.
23
+
24
+ If you modify the software, you must include in any modified copies of the software prominent notices stating that you have modified the software.
25
+
26
+ **No Other Rights**
27
+ These terms do not imply any licenses other than those expressly granted in these terms.
28
+
29
+ **Termination**
30
+ If you use the software in violation of these terms, such use is not licensed, and your licenses will automatically terminate. If the licensor provides you with a notice of your violation, and you cease all violation of this license no later than 30 days after you receive that notice, your licenses will be reinstated retroactively. However, if you violate these terms after such reinstatement, any additional violation of these terms will cause your licenses to terminate automatically and permanently.
31
+
32
+ **No Liability**
33
+ As far as the law allows, the software comes as is, without any warranty or condition, and the licensor will not be liable to you for any damages arising out of these terms or the use or nature of the software, under any kind of legal claim.
34
+
35
+ **Definitions**
36
+ The _licensor_ is the entity offering these terms, and the _software_ is the software the licensor makes available under these terms, including any portion of it.
37
+
38
+ _you_ refers to the individual or entity agreeing to these terms.
39
+
40
+ _your company_ is any legal entity, sole proprietorship, or other kind of organization that you work for, plus all organizations that have control over, are under the control of, or are under common control with that organization. _control_ means ownership of substantially all the assets of an entity, or the power to direct its management and policies by vote, contract, or otherwise. Control can be direct or indirect.
41
+
42
+ _your licenses_ are all the licenses granted to you for the software under these terms.
43
+
44
+ _use_ means anything you do with the software requiring one of your licenses.
45
+
46
+ _trademark_ means trademarks, service marks, and similar rights.
package/README.md ADDED
@@ -0,0 +1,140 @@
1
+ # @mastra/mongodb
2
+
3
+ MongoDB Atlas Search implementation for Mastra, providing vector similarity search and index management using MongoDB Atlas Local or Atlas Cloud.
4
+
5
+ ## Installation
6
+
7
+ ```bash
8
+ npm install @mastra/mongodb
9
+ ```
10
+
11
+ ## Prerequisites
12
+
13
+ - MongoDB Atlas Local (via Docker) or MongoDB Atlas Cloud instance with Atlas Search enabled
14
+ - MongoDB 7.0+ recommended
15
+
16
+ ## Usage
17
+
18
+ ### Vector Store
19
+
20
+ ```typescript
21
+ import { MongoDBVector } from '@mastra/mongodb';
22
+
23
+ const vectorDB = new MongoDBVector({
24
+ uri: 'mongodb://mongodb:mongodb@localhost:27018/?authSource=admin&directConnection=true',
25
+ dbName: 'vector_db',
26
+ });
27
+
28
+ // Connect to MongoDB
29
+ await vectorDB.connect();
30
+
31
+ // Create a new vector index (collection)
32
+ await vectorDB.createIndex({
33
+ indexName: 'my_vectors',
34
+ dimension: 1536,
35
+ metric: 'cosine', // or 'euclidean', 'dotproduct'
36
+ });
37
+
38
+ // Upsert vectors
39
+ const ids = await vectorDB.upsert({
40
+ indexName: 'my_vectors',
41
+ vectors: [[0.1, 0.2, ...], [0.3, 0.4, ...]],
42
+ metadata: [{ text: 'doc1' }, { text: 'doc2' }],
43
+ });
44
+
45
+ // Query vectors
46
+ const results = await vectorDB.query({
47
+ indexName: 'my_vectors',
48
+ queryVector: [0.1, 0.2, ...],
49
+ topK: 10,
50
+ filter: { text: 'doc1' },
51
+ includeVector: false,
52
+ minScore: 0.5,
53
+ });
54
+
55
+ // Clean up
56
+ await vectorDB.disconnect();
57
+ ```
58
+
59
+ ## Configuration
60
+
61
+ The MongoDB vector store is initialized with:
62
+
63
+ - `uri`: MongoDB connection string (with credentials and options)
64
+ - `dbName`: Name of the database to use
65
+
66
+ Example:
67
+
68
+ ```typescript
69
+ const vectorDB = new MongoDBVector({
70
+ uri: 'mongodb://mongodb:mongodb@localhost:27018/?authSource=admin&directConnection=true',
71
+ dbName: 'vector_db',
72
+ });
73
+ ```
74
+
75
+ ## Features
76
+
77
+ ### Vector Store Features
78
+
79
+ - Vector similarity search with cosine, euclidean, and dotproduct metrics (Atlas Search)
80
+ - Metadata filtering with MongoDB-style query syntax
81
+ - Minimum score threshold for queries
82
+ - Automatic UUID generation for vectors
83
+ - Collection (index) management: create, list, describe, delete
84
+ - Atlas Search readiness checks for reliable testing
85
+
86
+ ## Supported Filter Operators
87
+
88
+ - Comparison: `$eq`, `$ne`, `$gt`, `$gte`, `$lt`, `$lte`
89
+ - Logical: `$and`, `$or`
90
+ - Array: `$in`, `$nin`
91
+ - Text: `$regex`, `$like`
92
+
93
+ Example filter:
94
+
95
+ ```typescript
96
+ {
97
+ $and: [{ age: { $gt: 25 } }, { tags: { $in: ['tag1', 'tag2'] } }];
98
+ }
99
+ ```
100
+
101
+ ## Distance Metrics
102
+
103
+ The following distance metrics are supported:
104
+
105
+ - `cosine` → Cosine similarity (default)
106
+ - `euclidean` → Euclidean distance
107
+ - `dotproduct` → Dot product
108
+
109
+ ## Vector Store Methods
110
+
111
+ - `createIndex({indexName, dimension, metric})`: Create a new collection with vector search support
112
+ - `upsert({indexName, vectors, metadata?, ids?})`: Add or update vectors
113
+ - `query({indexName, queryVector, topK?, filter?, includeVector?, minScore?, documentFilter?})`: Search for similar vectors (optionally filter by document content)
114
+
115
+ > **Note:** `documentFilter` allows filtering results based on the content of the `document` field. Example: `{ $contains: 'specific text' }` will return only vectors whose associated document contains the specified text.
116
+
117
+ - `listIndexes()`: List all vector-enabled collections
118
+ - `describeIndex(indexName)`: Get collection statistics (dimension, count, metric)
119
+ - `updateIndexById(indexName, id, { vector?, metadata? })`: Update a vector and/or its metadata by ID
120
+ - `deleteIndexById(indexName, id)`: Delete a vector by ID
121
+ - `deleteIndex(indexName)`: Delete a collection
122
+ - `disconnect()`: Close the MongoDB connection
123
+
124
+ ## Query Response Format
125
+
126
+ Each query result includes:
127
+
128
+ - `id`: Vector ID
129
+ - `score`: Similarity score (higher is more similar)
130
+ - `metadata`: Associated metadata
131
+ - `vector`: Original vector (if `includeVector` is true)
132
+
133
+ ## Testing
134
+
135
+ Integration tests use MongoDB Atlas Local via Docker. See `docker-compose.yml` for setup. The test suite includes readiness checks for Atlas Search before running vector operations.
136
+
137
+ ## Related Links
138
+
139
+ - [MongoDB Atlas Search Documentation](https://www.mongodb.com/docs/atlas/atlas-search/)
140
+ - [MongoDB Node.js Driver](https://mongodb.github.io/node-mongodb-native/)
@@ -0,0 +1,88 @@
1
+ import { BaseFilterTranslator } from '@mastra/core/vector/filter';
2
+ import type { CreateIndexParams } from '@mastra/core/vector';
3
+ import type { IndexStats } from '@mastra/core/vector';
4
+ import { MastraVector } from '@mastra/core/vector';
5
+ import type { MongoClientOptions } from 'mongodb';
6
+ import type { OperatorSupport } from '@mastra/core/vector/filter';
7
+ import type { ParamsToArgs } from '@mastra/core/vector';
8
+ import type { QueryResult } from '@mastra/core/vector';
9
+ import type { QueryVectorArgs } from '@mastra/core/vector';
10
+ import type { QueryVectorParams } from '@mastra/core/vector';
11
+ import type { UpsertVectorArgs } from '@mastra/core/vector';
12
+ import type { UpsertVectorParams } from '@mastra/core/vector';
13
+ import type { VectorFilter } from '@mastra/core/vector/filter';
14
+
15
+ /**
16
+ * Translator for MongoDB filter queries.
17
+ * Maintains MongoDB-compatible syntax while ensuring proper validation
18
+ * and normalization of values.
19
+ */
20
+ export declare class MongoDBFilterTranslator extends BaseFilterTranslator {
21
+ protected getSupportedOperators(): OperatorSupport;
22
+ translate(filter?: VectorFilter): any;
23
+ private translateNode;
24
+ private translateOperatorValue;
25
+ isEmpty(filter: any): boolean;
26
+ }
27
+
28
+ declare type MongoDBQueryArgs = [...QueryVectorArgs, string?];
29
+ export { MongoDBQueryArgs }
30
+ export { MongoDBQueryArgs as MongoDBQueryArgs_alias_1 }
31
+
32
+ declare interface MongoDBQueryVectorParams extends QueryVectorParams {
33
+ documentFilter?: VectorFilter;
34
+ }
35
+ export { MongoDBQueryVectorParams }
36
+ export { MongoDBQueryVectorParams as MongoDBQueryVectorParams_alias_1 }
37
+
38
+ declare type MongoDBUpsertArgs = [...UpsertVectorArgs, string[]?];
39
+ export { MongoDBUpsertArgs }
40
+ export { MongoDBUpsertArgs as MongoDBUpsertArgs_alias_1 }
41
+
42
+ declare type MongoDBUpsertParams = ParamsToArgs<MongoDBUpsertArgs>;
43
+ export { MongoDBUpsertParams }
44
+ export { MongoDBUpsertParams as MongoDBUpsertParams_alias_1 }
45
+
46
+ declare interface MongoDBUpsertVectorParams extends UpsertVectorParams {
47
+ documents?: string[];
48
+ }
49
+ export { MongoDBUpsertVectorParams }
50
+ export { MongoDBUpsertVectorParams as MongoDBUpsertVectorParams_alias_1 }
51
+
52
+ declare class MongoDBVector extends MastraVector {
53
+ private client;
54
+ private db;
55
+ private collections;
56
+ private readonly embeddingFieldName;
57
+ private readonly metadataFieldName;
58
+ private readonly documentFieldName;
59
+ private collectionForValidation;
60
+ private mongoMetricMap;
61
+ constructor({ uri, dbName, options }: {
62
+ uri: string;
63
+ dbName: string;
64
+ options?: MongoClientOptions;
65
+ });
66
+ connect(): Promise<void>;
67
+ disconnect(): Promise<void>;
68
+ createIndex(params: CreateIndexParams): Promise<void>;
69
+ waitForIndexReady(indexName: string, timeoutMs?: number, checkIntervalMs?: number): Promise<void>;
70
+ upsert(params: MongoDBUpsertVectorParams): Promise<string[]>;
71
+ query(params: MongoDBQueryVectorParams): Promise<QueryResult[]>;
72
+ listIndexes(): Promise<string[]>;
73
+ describeIndex(indexName: string): Promise<IndexStats>;
74
+ deleteIndex(indexName: string): Promise<void>;
75
+ updateIndexById(indexName: string, id: string, update: {
76
+ vector?: number[];
77
+ metadata?: Record<string, any>;
78
+ }): Promise<void>;
79
+ deleteIndexById(indexName: string, id: string): Promise<void>;
80
+ private getCollection;
81
+ private validateVectorDimensions;
82
+ private setIndexDimension;
83
+ private transformFilter;
84
+ }
85
+ export { MongoDBVector }
86
+ export { MongoDBVector as MongoDBVector_alias_1 }
87
+
88
+ export { }
@@ -0,0 +1,88 @@
1
+ import { BaseFilterTranslator } from '@mastra/core/vector/filter';
2
+ import type { CreateIndexParams } from '@mastra/core/vector';
3
+ import type { IndexStats } from '@mastra/core/vector';
4
+ import { MastraVector } from '@mastra/core/vector';
5
+ import type { MongoClientOptions } from 'mongodb';
6
+ import type { OperatorSupport } from '@mastra/core/vector/filter';
7
+ import type { ParamsToArgs } from '@mastra/core/vector';
8
+ import type { QueryResult } from '@mastra/core/vector';
9
+ import type { QueryVectorArgs } from '@mastra/core/vector';
10
+ import type { QueryVectorParams } from '@mastra/core/vector';
11
+ import type { UpsertVectorArgs } from '@mastra/core/vector';
12
+ import type { UpsertVectorParams } from '@mastra/core/vector';
13
+ import type { VectorFilter } from '@mastra/core/vector/filter';
14
+
15
+ /**
16
+ * Translator for MongoDB filter queries.
17
+ * Maintains MongoDB-compatible syntax while ensuring proper validation
18
+ * and normalization of values.
19
+ */
20
+ export declare class MongoDBFilterTranslator extends BaseFilterTranslator {
21
+ protected getSupportedOperators(): OperatorSupport;
22
+ translate(filter?: VectorFilter): any;
23
+ private translateNode;
24
+ private translateOperatorValue;
25
+ isEmpty(filter: any): boolean;
26
+ }
27
+
28
+ declare type MongoDBQueryArgs = [...QueryVectorArgs, string?];
29
+ export { MongoDBQueryArgs }
30
+ export { MongoDBQueryArgs as MongoDBQueryArgs_alias_1 }
31
+
32
+ declare interface MongoDBQueryVectorParams extends QueryVectorParams {
33
+ documentFilter?: VectorFilter;
34
+ }
35
+ export { MongoDBQueryVectorParams }
36
+ export { MongoDBQueryVectorParams as MongoDBQueryVectorParams_alias_1 }
37
+
38
+ declare type MongoDBUpsertArgs = [...UpsertVectorArgs, string[]?];
39
+ export { MongoDBUpsertArgs }
40
+ export { MongoDBUpsertArgs as MongoDBUpsertArgs_alias_1 }
41
+
42
+ declare type MongoDBUpsertParams = ParamsToArgs<MongoDBUpsertArgs>;
43
+ export { MongoDBUpsertParams }
44
+ export { MongoDBUpsertParams as MongoDBUpsertParams_alias_1 }
45
+
46
+ declare interface MongoDBUpsertVectorParams extends UpsertVectorParams {
47
+ documents?: string[];
48
+ }
49
+ export { MongoDBUpsertVectorParams }
50
+ export { MongoDBUpsertVectorParams as MongoDBUpsertVectorParams_alias_1 }
51
+
52
+ declare class MongoDBVector extends MastraVector {
53
+ private client;
54
+ private db;
55
+ private collections;
56
+ private readonly embeddingFieldName;
57
+ private readonly metadataFieldName;
58
+ private readonly documentFieldName;
59
+ private collectionForValidation;
60
+ private mongoMetricMap;
61
+ constructor({ uri, dbName, options }: {
62
+ uri: string;
63
+ dbName: string;
64
+ options?: MongoClientOptions;
65
+ });
66
+ connect(): Promise<void>;
67
+ disconnect(): Promise<void>;
68
+ createIndex(params: CreateIndexParams): Promise<void>;
69
+ waitForIndexReady(indexName: string, timeoutMs?: number, checkIntervalMs?: number): Promise<void>;
70
+ upsert(params: MongoDBUpsertVectorParams): Promise<string[]>;
71
+ query(params: MongoDBQueryVectorParams): Promise<QueryResult[]>;
72
+ listIndexes(): Promise<string[]>;
73
+ describeIndex(indexName: string): Promise<IndexStats>;
74
+ deleteIndex(indexName: string): Promise<void>;
75
+ updateIndexById(indexName: string, id: string, update: {
76
+ vector?: number[];
77
+ metadata?: Record<string, any>;
78
+ }): Promise<void>;
79
+ deleteIndexById(indexName: string, id: string): Promise<void>;
80
+ private getCollection;
81
+ private validateVectorDimensions;
82
+ private setIndexDimension;
83
+ private transformFilter;
84
+ }
85
+ export { MongoDBVector }
86
+ export { MongoDBVector as MongoDBVector_alias_1 }
87
+
88
+ export { }