@@ -156,11 +156,10 @@ def _get_project_projection(response_model: Type[T]):
156
156
new_collection_name = new_collections .get (old_name , old_name )
157
157
project_projection = {
158
158
"_id" : False ,
159
+ "timestamp" : "$timestamp" ,
159
160
}
160
161
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" :
164
163
project_projection ["unit" ] = f"${ new_collection_name } _unit"
165
164
else :
166
165
project_projection [field_name ] = f"${ new_collection_name } "
@@ -204,6 +203,7 @@ def create_nested_pipeline(model: Type[BaseModel], prefix=""):
204
203
match_conditions = {}
205
204
pipeline = {
206
205
"_id" : False ,
206
+ "$timestamp" : "$timestamp" ,
207
207
}
208
208
209
209
for field_name , field_type in model .__fields__ .items ():
@@ -213,12 +213,13 @@ def create_nested_pipeline(model: Type[BaseModel], prefix=""):
213
213
mongo_field = new_collections .get (lookup_field , field_name .lower ())
214
214
full_field_name = f"{ prefix } { mongo_field } "
215
215
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 }
220
220
else :
221
221
pipeline [full_field_name ] = f"${ full_field_name } "
222
+ match_conditions [full_field_name ] = {"$exists" : True }
222
223
223
224
if field_name in nested_fields :
224
225
if get_origin (field_type .type_ ) is List :
@@ -243,9 +244,6 @@ def create_nested_pipeline(model: Type[BaseModel], prefix=""):
243
244
match_conditions .update (
244
245
{f"{ full_field_name } .{ k } " : v for k , v in nested_match .items ()}
245
246
)
246
- else :
247
- pipeline [field_name ] = f"${ full_field_name } "
248
- match_conditions [full_field_name ] = {"$exists" : True }
249
247
250
248
logger .debug (f"Field: { field_name } , Full field name: { full_field_name } " )
251
249
logger .debug (f"Resulting pipeline part: { pipeline [field_name ]} " )
0 commit comments