From 957cc0c2129cbba021a92a11b49048bb1543c862 Mon Sep 17 00:00:00 2001 From: Mauro Andre Date: Tue, 14 Jan 2025 17:39:48 -0300 Subject: [PATCH] =?UTF-8?q?=F0=9F=90=9B=20Fix=20group=20stage=20in=20pipel?= =?UTF-8?q?ine?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pyodmongo/services/aggregate_stages.py | 4 ++-- pyodmongo/services/reference_pipeline.py | 14 +++++++------- requirements.txt | 4 ++-- 3 files changed, 11 insertions(+), 11 deletions(-) diff --git a/pyodmongo/services/aggregate_stages.py b/pyodmongo/services/aggregate_stages.py index ce5bbf2..3bdcd81 100644 --- a/pyodmongo/services/aggregate_stages.py +++ b/pyodmongo/services/aggregate_stages.py @@ -100,7 +100,7 @@ def set_(local_field: str, as_: str): return pipeline -def group_set_replace_root(id_: str, array_index: str, field: str, path_str: str): +def group_set_replace_root(id_: list[str], array_index: str, field: str, path_str: str): """ Constructs a combination of group, set, and replaceRoot stages for a MongoDB aggregation pipeline. @@ -120,7 +120,7 @@ def group_set_replace_root(id_: str, array_index: str, field: str, path_str: str return [ { "$group": { - "_id": f"${id_}", + "_id": id_, "_document": {"$first": "$$ROOT"}, field: {"$push": f"${path_str}"}, } diff --git a/pyodmongo/services/reference_pipeline.py b/pyodmongo/services/reference_pipeline.py index 4d409ab..4158a1a 100644 --- a/pyodmongo/services/reference_pipeline.py +++ b/pyodmongo/services/reference_pipeline.py @@ -66,7 +66,7 @@ def resolve_reference_pipeline( ) for db_field_path in paths: path_str = "" - unwind_index_list = [] + unwind_index_list = ["_id"] paths_str_to_group = [] db_field: DbField = None for index, db_field in enumerate(db_field_path): @@ -99,16 +99,16 @@ def resolve_reference_pipeline( pipeline += set_(local_field=path_str, as_=path_str) for index, path_str in enumerate(reversed(paths_str_to_group)): - reverse_index = len(paths_str_to_group) - index - 1 - unwind_index = ( - "_id" if reverse_index - 1 < 0 else unwind_index_list[reverse_index - 1] - ) + id_ = [ + f"${e}" for e in unwind_index_list[: len(unwind_index_list) - index - 1] + ] + to_unset = unwind_index_list[-(index + 1)] pipeline += group_set_replace_root( - id_=unwind_index, + id_=[id_], array_index=unwind_index_list[-1], field=path_str.split(".")[-1], path_str=path_str, ) - pipeline += unset(fields=[unwind_index_list[reverse_index - 1]]) + pipeline += unset(fields=[to_unset]) return pipeline diff --git a/requirements.txt b/requirements.txt index 1a8311b..031b8e5 100644 --- a/requirements.txt +++ b/requirements.txt @@ -47,8 +47,8 @@ pillow==10.3.0 platformdirs==4.2.2 pluggy==1.5.0 pycparser==2.22 -pydantic==2.10.0 -pydantic_core==2.27.0 +pydantic==2.10.5 +pydantic_core==2.27.2 Pygments==2.18.0 pymdown-extensions==10.8.1 pymongo==4.9.2