Skip to content

Commit 2010833

Browse files
committed
Leverage the project projection which handles units.
1 parent 93f9847 commit 2010833

File tree

1 file changed

+8
-10
lines changed

1 file changed

+8
-10
lines changed

opensensor/collection_apis.py

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -156,11 +156,10 @@ def _get_project_projection(response_model: Type[T]):
156156
new_collection_name = new_collections.get(old_name, old_name)
157157
project_projection = {
158158
"_id": False,
159+
"timestamp": "$timestamp",
159160
}
160161
for field_name, _ in response_model.__fields__.items():
161-
if field_name == "timestamp":
162-
project_projection["timestamp"] = "$timestamp"
163-
elif field_name == "unit":
162+
if field_name == "unit":
164163
project_projection["unit"] = f"${new_collection_name}_unit"
165164
else:
166165
project_projection[field_name] = f"${new_collection_name}"
@@ -204,6 +203,7 @@ def create_nested_pipeline(model: Type[BaseModel], prefix=""):
204203
match_conditions = {}
205204
pipeline = {
206205
"_id": False,
206+
"$timestamp": "$timestamp",
207207
}
208208

209209
for field_name, field_type in model.__fields__.items():
@@ -213,12 +213,13 @@ def create_nested_pipeline(model: Type[BaseModel], prefix=""):
213213
mongo_field = new_collections.get(lookup_field, field_name.lower())
214214
full_field_name = f"{prefix}{mongo_field}"
215215

216-
if field_name == "timestamp":
217-
pipeline["timestamp"] = "$timestamp"
218-
elif field_name == "unit":
219-
pipeline["unit"] = f"${full_field_name}_unit"
216+
if field_name == "unit":
217+
unit_field_name = f"{prefix}{mongo_field}_unit"
218+
pipeline["unit"] = f"${unit_field_name}"
219+
match_conditions[unit_field_name] = {"$exists": True}
220220
else:
221221
pipeline[full_field_name] = f"${full_field_name}"
222+
match_conditions[full_field_name] = {"$exists": True}
222223

223224
if field_name in nested_fields:
224225
if get_origin(field_type.type_) is List:
@@ -243,9 +244,6 @@ def create_nested_pipeline(model: Type[BaseModel], prefix=""):
243244
match_conditions.update(
244245
{f"{full_field_name}.{k}": v for k, v in nested_match.items()}
245246
)
246-
else:
247-
pipeline[field_name] = f"${full_field_name}"
248-
match_conditions[full_field_name] = {"$exists": True}
249247

250248
logger.debug(f"Field: {field_name}, Full field name: {full_field_name}")
251249
logger.debug(f"Resulting pipeline part: {pipeline[field_name]}")

0 commit comments

Comments
 (0)