From 773efc5cc78e6278b2ccdc3caab822eb9d5f1d58 Mon Sep 17 00:00:00 2001 From: Matt Davis Date: Wed, 2 Oct 2024 00:40:13 -0400 Subject: [PATCH] attempt to get relay nested model retrieval --- opensensor/collection_apis.py | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/opensensor/collection_apis.py b/opensensor/collection_apis.py index b4993a3..0876322 100644 --- a/opensensor/collection_apis.py +++ b/opensensor/collection_apis.py @@ -200,6 +200,7 @@ def get_nested_fields(model: Type[BaseModel]): def create_nested_pipeline(model: Type[BaseModel], prefix=""): + logger.debug(f"Creating nested pipeline for model: {model.__name__}, prefix: {prefix}") nested_fields = get_nested_fields(model) pipeline = {} match_conditions = {} @@ -218,10 +219,16 @@ def create_nested_pipeline(model: Type[BaseModel], prefix=""): if field_name in nested_fields: if get_origin(field_type.type_) is List: nested_pipeline, nested_match = create_nested_pipeline( - nested_fields[field_name], "$$item." + nested_fields[field_name], "" # Empty prefix for list items ) pipeline[field_name] = { - "$map": {"input": f"${full_field_name}", "as": "item", "in": nested_pipeline} + "$map": { + "input": f"${full_field_name}", + "as": "item", + "in": { + k: f"$item.{v.replace('$', '')}" for k, v in nested_pipeline.items() + }, + } } match_conditions[full_field_name] = {"$exists": True, "$ne": []} else: @@ -236,6 +243,10 @@ def create_nested_pipeline(model: Type[BaseModel], prefix=""): pipeline[field_name] = f"${full_field_name}" match_conditions[full_field_name] = {"$exists": True} + logger.debug(f"Field: {field_name}, Full field name: {full_field_name}") + logger.debug(f"Resulting pipeline part: {pipeline[field_name]}") + + logger.debug(f"Final pipeline for {model.__name__}: {pipeline}") return pipeline, match_conditions