Skip to content

Commit 9d5f8a7

Browse files
committed
Leverage the project projection which handles units.
1 parent 541c72b commit 9d5f8a7

File tree

1 file changed

+10
-5
lines changed

1 file changed

+10
-5
lines changed

opensensor/collection_apis.py

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -201,20 +201,25 @@ def get_nested_fields(model: Type[BaseModel]):
201201
def create_nested_pipeline(model: Type[BaseModel], prefix=""):
202202
logger.debug(f"Creating nested pipeline for model: {model.__name__}, prefix: {prefix}")
203203
nested_fields = get_nested_fields(model)
204-
pipeline = _get_project_projection(model)
205204
match_conditions = {}
205+
pipeline = {
206+
"_id": False,
207+
}
206208

207209
for field_name, field_type in model.__fields__.items():
208-
if field_name in pipeline:
209-
# Skip fields that are already in the pipeline
210-
continue
211-
212210
lookup_field = (
213211
model.collection_name() if hasattr(model, "collection_name") else model.__name__
214212
)
215213
mongo_field = new_collections.get(lookup_field, field_name.lower())
216214
full_field_name = f"{prefix}{mongo_field}"
217215

216+
if field_name == "timestamp":
217+
pipeline["timestamp"] = "$timestamp"
218+
elif field_name == "unit":
219+
pipeline["unit"] = f"${full_field_name}_unit"
220+
else:
221+
pipeline[full_field_name] = f"${full_field_name}"
222+
218223
if field_name in nested_fields:
219224
if get_origin(field_type.type_) is List:
220225
nested_pipeline, nested_match = create_nested_pipeline(

0 commit comments

Comments
 (0)