Skip to content

Commit

Permalink
Merge pull request #155 from mauro-andre/fix_pipeline_group
Browse files Browse the repository at this point in the history
🐛 Fix group stage in pipeline
  • Loading branch information
mauro-andre authored Jan 14, 2025
2 parents 9a4620b + 957cc0c commit 2fcdc1c
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 11 deletions.
4 changes: 2 additions & 2 deletions pyodmongo/services/aggregate_stages.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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}"},
}
Expand Down
14 changes: 7 additions & 7 deletions pyodmongo/services/reference_pipeline.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -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
4 changes: 2 additions & 2 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 2fcdc1c

Please sign in to comment.