@@ -24,21 +24,74 @@ function toMongoDBFilter(
24
24
return filters ;
25
25
}
26
26
27
- // MongoDB Atlas Vector Store class implementing VectorStore
27
+ /**
28
+ * Vector store that uses MongoDB Atlas for storage and vector search.
29
+ * This store uses the $vectorSearch aggregation stage to perform vector similarity search.
30
+ */
28
31
export class MongoDBAtlasVectorSearch
29
32
extends VectorStoreBase
30
33
implements VectorStoreNoEmbedModel
31
34
{
32
35
storesText : boolean = true ;
33
36
flatMetadata : boolean = true ;
34
37
38
+ /**
39
+ * The used MongoClient. If not given, a new MongoClient is created based on the MONGODB_URI env variable.
40
+ */
35
41
mongodbClient : MongoClient ;
42
+
43
+ /**
44
+ * Name of the vector index. If invalid, Mongo will silently ignore this issue and return 0 results.
45
+ *
46
+ * Default: "default"
47
+ */
36
48
indexName : string ;
49
+
50
+ /**
51
+ * Name of the key containing the embedding vector.
52
+ *
53
+ * Default: "embedding"
54
+ */
37
55
embeddingKey : string ;
56
+
57
+ /**
58
+ * Name of the key containing the node id.
59
+ *
60
+ * Default: "id"
61
+ */
38
62
idKey : string ;
63
+
64
+ /**
65
+ * Name of the key containing the node text.
66
+ *
67
+ * Default: "text"
68
+ */
39
69
textKey : string ;
70
+
71
+ /**
72
+ * Name of the key containing the node metadata.
73
+ *
74
+ * Default: "metadata"
75
+ */
40
76
metadataKey : string ;
77
+
78
+ /**
79
+ * Options to pass to the insertMany function when adding nodes.
80
+ */
41
81
insertOptions ?: BulkWriteOptions ;
82
+
83
+ /**
84
+ * Function to determine the number of candidates to retrieve for a given query.
85
+ * In case your results are not good, you might tune this value.
86
+ *
87
+ * {@link https://www.mongodb.com/docs/atlas/atlas-vector-search/vector-search-stage/|Run Vector Search Queries}
88
+ *
89
+ * {@link https://arxiv.org/abs/1603.09320|Efficient and robust approximate nearest neighbor search using Hierarchical Navigable Small World graphs}
90
+ *
91
+ *
92
+ * Default: query.similarityTopK * 10
93
+ */
94
+ numCandidates : ( query : VectorStoreQuery ) => number ;
42
95
private collection : Collection ;
43
96
44
97
constructor (
@@ -69,9 +122,17 @@ export class MongoDBAtlasVectorSearch
69
122
this . idKey = init . idKey ?? "id" ;
70
123
this . textKey = init . textKey ?? "text" ;
71
124
this . metadataKey = init . metadataKey ?? "metadata" ;
125
+ this . numCandidates =
126
+ init . numCandidates ?? ( ( query ) => query . similarityTopK * 10 ) ;
72
127
this . insertOptions = init . insertOptions ;
73
128
}
74
129
130
+ /**
131
+ * Add nodes to the vector store.
132
+ *
133
+ * @param nodes Nodes to add to the vector store
134
+ * @returns List of node ids that were added
135
+ */
75
136
async add ( nodes : BaseNode [ ] ) : Promise < string [ ] > {
76
137
if ( ! nodes || nodes . length === 0 ) {
77
138
return [ ] ;
@@ -101,6 +162,12 @@ export class MongoDBAtlasVectorSearch
101
162
return nodes . map ( ( node ) => node . id_ ) ;
102
163
}
103
164
165
+ /**
166
+ * Delete nodes from the vector store with the given redDocId.
167
+ *
168
+ * @param refDocId The refDocId of the nodes to delete
169
+ * @param deleteOptions Options to pass to the deleteOne function
170
+ */
104
171
async delete ( refDocId : string , deleteOptions ?: any ) : Promise < void > {
105
172
await this . collection . deleteMany (
106
173
{
@@ -114,14 +181,20 @@ export class MongoDBAtlasVectorSearch
114
181
return this . mongodbClient ;
115
182
}
116
183
184
+ /**
185
+ * Perform a vector similarity search query.
186
+ *
187
+ * @param query The query to run
188
+ * @returns List of nodes and their similarities
189
+ */
117
190
async query (
118
191
query : VectorStoreQuery ,
119
192
options ?: any ,
120
193
) : Promise < VectorStoreQueryResult > {
121
194
const params : any = {
122
195
queryVector : query . queryEmbedding ,
123
196
path : this . embeddingKey ,
124
- numCandidates : query . similarityTopK * 10 ,
197
+ numCandidates : this . numCandidates ( query ) ,
125
198
limit : query . similarityTopK ,
126
199
index : this . indexName ,
127
200
} ;
0 commit comments