Skip to content

Commit

Permalink
mongodb[minor]: add, implement delete method (#5559)
Browse files Browse the repository at this point in the history
* 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 <guillermo.c.martinez@dcsl.com>
  • Loading branch information
telekosmos and Guille authored Jun 4, 2024
1 parent 74cb905 commit d6b29b4
Showing 1 changed file with 16 additions and 1 deletion.
17 changes: 16 additions & 1 deletion libs/langchain-mongodb/src/vectorstores.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import type { Collection, Document as MongoDBDocument } from "mongodb";
import { type Collection, type Document as MongoDBDocument } from "mongodb";
import {
MaxMarginalRelevanceSearchOptions,
VectorStore,
} from "@langchain/core/vectorstores";
import type { EmbeddingsInterface } from "@langchain/core/embeddings";
import { chunkArray } from "@langchain/core/utils/chunk_array";
import { Document } from "@langchain/core/documents";
import { maximalMarginalRelevance } from "@langchain/core/utils/math";
import {
Expand Down Expand Up @@ -256,6 +257,20 @@ export class MongoDBAtlasVectorSearch extends VectorStore {
});
}

/**
* Delete documents from the collection
* @param ids - An array of document IDs to be deleted from the collection.
*
* @returns - A promise that resolves when all documents deleted
*/
// eslint-disable-next-line @typescript-eslint/no-explicit-any
async delete(params: { ids: any[] }): Promise<void> {
const CHUNK_SIZE = 50;
const chunkIds: any[][] = chunkArray(params.ids, CHUNK_SIZE); // eslint-disable-line @typescript-eslint/no-explicit-any
for (const chunk of chunkIds)
await this.collection.deleteMany({ _id: { $in: chunk } });
}

/**
* Static method to create an instance of MongoDBAtlasVectorSearch from a
* list of texts. It first converts the texts to vectors and then adds
Expand Down

0 comments on commit d6b29b4

Please sign in to comment.