diff --git a/pyodmongo/services/aggregate_stages.py b/pyodmongo/services/aggregate_stages.py index 3c71a29..f3ffc0c 100644 --- a/pyodmongo/services/aggregate_stages.py +++ b/pyodmongo/services/aggregate_stages.py @@ -134,7 +134,8 @@ def group_set_replace_root( f"_document.{path_str}": { "$cond": { "if": {"$eq": [f"$_document.{array_index}", None]}, - "then": None, + "then": [], + # "then": None, "else": f"${field}", } } diff --git a/pyodmongo/services/reference_pipeline.py b/pyodmongo/services/reference_pipeline.py index 2fc798d..557ab79 100644 --- a/pyodmongo/services/reference_pipeline.py +++ b/pyodmongo/services/reference_pipeline.py @@ -103,12 +103,13 @@ def resolve_reference_pipeline( id_ = [ f"${e}" for e in unwind_index_list[: len(unwind_index_list) - index - 1] ] - index_to_unset.append(unwind_index_list[-(index + 1)]) + current_index = unwind_index_list[-(index + 1)] + index_to_unset.append(current_index) to_sort = {key: 1 for key in unwind_index_list[1:]} pipeline += group_set_replace_root( to_sort=to_sort, id_=id_, - array_index=unwind_index_list[-1], + array_index=current_index, field=path_str.split(".")[-1], path_str=path_str, ) diff --git a/tests/test_engines.py b/tests/test_engines.py index 8fb6146..78dd233 100644 --- a/tests/test_engines.py +++ b/tests/test_engines.py @@ -263,7 +263,7 @@ class B(MainBaseModel): class C(DbModel): name: str = "name_1" - b_list: list[B] | None = None + b_list: list[B] = [] _collection: ClassVar = "c" obj_c = C()