Skip to content

Commit 796daf4

Browse files
committed
Leverage the project projection which handles units.
1 parent 9a48deb commit 796daf4

File tree

1 file changed

+7
-2
lines changed

1 file changed

+7
-2
lines changed

opensensor/collection_apis.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -255,7 +255,6 @@ def create_nested_pipeline(model: Type[BaseModel], prefix=""):
255255
def create_model_instance(model: Type[BaseModel], data: dict):
256256
nested_fields = get_nested_fields(model)
257257

258-
# Handle flat models (like Pressure, LiquidLevel, pH) that have a single main field
259258
for field_name, _ in model.__fields__.items():
260259
if field_name == "timestamp":
261260
continue
@@ -267,8 +266,14 @@ def create_model_instance(model: Type[BaseModel], data: dict):
267266
)
268267
mongo_field = new_collections.get(lookup_field, field_name.lower())
269268

269+
# Special handling for the unit field
270+
if field_name == "unit":
271+
unit_field = f"{mongo_field}_unit"
272+
if unit_field in data:
273+
data[field_name] = data[unit_field]
274+
continue
270275
# Check if the mongo_field exists in the data
271-
if mongo_field in data:
276+
elif mongo_field in data:
272277
data[field_name] = data[mongo_field]
273278
elif field_name in data:
274279
# If the field_name exists in data, use it

0 commit comments

Comments
 (0)