I am using Mongodb Vector database with LangChain. I would like to add a metadata to each documents
and use the metadata to filter the results.
Can someone guide me?
Thanks for question. You can absolutely filter on metadata using Atlas Vector Search. The way you do this is by defining additional fields from your document that you’d like to filter on in the index.
Thanks!
I am working with Langchain, and the resource you provided worked for filtering the results for retrieval.
Followup question is:
How do I populate the vector database with custom metadata field ?
This is how I am adding the metadata
text_splitter = RecursiveCharacterTextSplitter(chunk_size=1000, chunk_overlap=100)
docs = text_splitter.split_documents(data)
# Help me find a better way than iterating over all the documents
for i, doc in enumerate(docs):
doc.metadata["user_id"] = user_id
MongoDBAtlasVectorSearch.from_documents(
documents=docs,
embedding=OpenAIEmbeddings(disallowed_special=()),
collection=MONGODB_COLLECTION,
index_name=self.config.search_index_name,
)