From e2824b721efd67a97da8c68861c816303d216cef Mon Sep 17 00:00:00 2001 From: yangdx Date: Sun, 29 Jun 2025 15:03:57 +0800 Subject: [PATCH] Fix LLM cache handling for MongoKVStorage to address document deletion scenarios. - Support fetching all "default_" prefixed documents - Maintain original behavior for other IDs - Return dictionary of documents for "default" - Keep backward compatibility --- lightrag/kg/mongo_impl.py | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/lightrag/kg/mongo_impl.py b/lightrag/kg/mongo_impl.py index 6483f6fd..a8dda1b9 100644 --- a/lightrag/kg/mongo_impl.py +++ b/lightrag/kg/mongo_impl.py @@ -98,7 +98,17 @@ class MongoKVStorage(BaseKVStorage): self._data = None async def get_by_id(self, id: str) -> dict[str, Any] | None: - return await self._data.find_one({"_id": id}) + if id == "default": + # Find all documents with _id starting with "default_" + cursor = self._data.find({"_id": {"$regex": "^default_"}}) + result = {} + async for doc in cursor: + # Use the complete _id as key + result[doc["_id"]] = doc + return result if result else None + else: + # Original behavior for non-"default" ids + return await self._data.find_one({"_id": id}) async def get_by_ids(self, ids: list[str]) -> list[dict[str, Any]]: cursor = self._data.find({"_id": {"$in": ids}})