Skip to content

Commit d6b29b4

Browse files
telekosmosGuille
andauthored
mongodb[minor]: add, implement delete method (#5559)
* feat: atlas search vector store delete method Method delete was missing in MongoDBAtlasVectorSearch class and it is needed if incremental indexing strategy is used * refactor: use built-in function for array chunking --------- Co-authored-by: Guille <[email protected]>
1 parent 74cb905 commit d6b29b4

File tree

1 file changed

+16
-1
lines changed

1 file changed

+16
-1
lines changed

libs/langchain-mongodb/src/vectorstores.ts

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
1-
import type { Collection, Document as MongoDBDocument } from "mongodb";
1+
import { type Collection, type Document as MongoDBDocument } from "mongodb";
22
import {
33
MaxMarginalRelevanceSearchOptions,
44
VectorStore,
55
} from "@langchain/core/vectorstores";
66
import type { EmbeddingsInterface } from "@langchain/core/embeddings";
7+
import { chunkArray } from "@langchain/core/utils/chunk_array";
78
import { Document } from "@langchain/core/documents";
89
import { maximalMarginalRelevance } from "@langchain/core/utils/math";
910
import {
@@ -256,6 +257,20 @@ export class MongoDBAtlasVectorSearch extends VectorStore {
256257
});
257258
}
258259

260+
/**
261+
* Delete documents from the collection
262+
* @param ids - An array of document IDs to be deleted from the collection.
263+
*
264+
* @returns - A promise that resolves when all documents deleted
265+
*/
266+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
267+
async delete(params: { ids: any[] }): Promise<void> {
268+
const CHUNK_SIZE = 50;
269+
const chunkIds: any[][] = chunkArray(params.ids, CHUNK_SIZE); // eslint-disable-line @typescript-eslint/no-explicit-any
270+
for (const chunk of chunkIds)
271+
await this.collection.deleteMany({ _id: { $in: chunk } });
272+
}
273+
259274
/**
260275
* Static method to create an instance of MongoDBAtlasVectorSearch from a
261276
* list of texts. It first converts the texts to vectors and then adds

0 commit comments

Comments
 (0)