From 7cebaba0a22c9b438e601dd6491f14f459706d2c Mon Sep 17 00:00:00 2001 From: Varun Chawla <34209028+veeceey@users.noreply.github.com> Date: Sun, 22 Mar 2026 23:50:17 -0700 Subject: [PATCH] fix: upgrade MongoDB vector store from deprecated knnVector to GA vectorSearch (#3995) Co-authored-by: kartik-mem0 Co-authored-by: Claude Opus 4.6 (1M context) --- mem0/vector_stores/mongodb.py | 23 +++++++++++------------ tests/vector_stores/test_mongodb.py | 21 ++++++++++----------- 2 files changed, 21 insertions(+), 23 deletions(-) diff --git a/mem0/vector_stores/mongodb.py b/mem0/vector_stores/mongodb.py index 2bdebf293..7d1e82bb6 100644 --- a/mem0/vector_stores/mongodb.py +++ b/mem0/vector_stores/mongodb.py @@ -26,7 +26,7 @@ class OutputData(BaseModel): class MongoDB(VectorStoreBase): - VECTOR_TYPE = "knnVector" + VECTOR_TYPE = "vector" SIMILARITY_METRIC = "cosine" def __init__(self, db_name: str, collection_name: str, embedding_model_dims: int, mongo_uri: str): @@ -69,17 +69,16 @@ class MongoDB(VectorStoreBase): else: search_index_model = SearchIndexModel( name=self.index_name, + type="vectorSearch", definition={ - "mappings": { - "dynamic": False, - "fields": { - "embedding": { - "type": self.VECTOR_TYPE, - "dimensions": self.embedding_model_dims, - "similarity": self.SIMILARITY_METRIC, - } - }, - } + "fields": [ + { + "type": self.VECTOR_TYPE, + "path": "embedding", + "numDimensions": self.embedding_model_dims, + "similarity": self.SIMILARITY_METRIC, + } + ] }, ) collection.create_search_index(search_index_model) @@ -141,7 +140,7 @@ class MongoDB(VectorStoreBase): "$vectorSearch": { "index": self.index_name, "limit": limit, - "numCandidates": limit, + "numCandidates": min(limit * 20, 10000), "queryVector": vectors, "path": "embedding", } diff --git a/tests/vector_stores/test_mongodb.py b/tests/vector_stores/test_mongodb.py index bb4b3f69f..d75a05846 100644 --- a/tests/vector_stores/test_mongodb.py +++ b/tests/vector_stores/test_mongodb.py @@ -48,17 +48,16 @@ def test_initalize_create_col(mongo_vector_fixture): search_index_model = args[0].document assert search_index_model == { "name": "test_collection_vector_index", + "type": "vectorSearch", "definition": { - "mappings": { - "dynamic": False, - "fields": { - "embedding": { - "type": "knnVector", - "dimensions": 1536, - "similarity": "cosine", - } - }, - } + "fields": [ + { + "type": "vector", + "path": "embedding", + "numDimensions": 1536, + "similarity": "cosine", + } + ] }, } assert mongo_vector.collection == mock_collection @@ -95,7 +94,7 @@ def test_search(mongo_vector_fixture): "$vectorSearch": { "index": "test_collection_vector_index", "limit": 2, - "numCandidates": 2, + "numCandidates": 40, "queryVector": query_vector, "path": "embedding", },