@@ -228,15 +228,27 @@ def create_nested_pipeline(model: Type[BaseModel], prefix=""):
228
228
def create_model_instance (model : Type [BaseModel ], data : dict ):
229
229
nested_fields = get_nested_fields (model )
230
230
231
- # Handle flat models (like Pressure) that have a single main field
231
+ # Handle flat models (like Pressure, LiquidLevel, pH ) that have a single main field
232
232
if len (model .__fields__ ) == 2 and "timestamp" in model .__fields__ :
233
233
main_field = next (field for field in model .__fields__ if field != "timestamp" )
234
234
lookup_field = (
235
- model .collection_name if hasattr (model , "collection_name" ) else model .__name__
235
+ model .collection_name () if hasattr (model , "collection_name" ) else model .__name__
236
236
)
237
- mongo_field = new_collections .get (lookup_field , main_field )
238
- if main_field not in data and mongo_field in data :
237
+ mongo_field = new_collections .get (lookup_field , main_field .lower ())
238
+
239
+ # Check if the mongo_field exists in the data
240
+ if mongo_field in data :
239
241
data [main_field ] = data [mongo_field ]
242
+ elif main_field .lower () in data :
243
+ # If the main_field (lowercase) exists in data, use it
244
+ data [main_field ] = data [main_field .lower ()]
245
+ else :
246
+ # If neither the mongo_field nor the main_field exists, log an error
247
+ logger .error (
248
+ f"Field '{ mongo_field } ' or '{ main_field } ' not found in data for model { model .__name__ } "
249
+ )
250
+ logger .error (f"Available fields in data: { list (data .keys ())} " )
251
+ # You might want to set a default value or raise an exception here
240
252
241
253
for field_name , nested_model in nested_fields .items ():
242
254
if field_name in data :
@@ -350,10 +362,15 @@ def get_uniform_sample_pipeline(
350
362
project_pipeline = create_nested_pipeline (response_model )
351
363
project_pipeline ["timestamp" ] = "$timestamp"
352
364
353
- # Handle flat models (like Pressure) that have a single main field
365
+ # Handle flat models (like Pressure, LiquidLevel, pH ) that have a single main field
354
366
if len (response_model .__fields__ ) == 2 and "timestamp" in response_model .__fields__ :
355
367
main_field = next (field for field in response_model .__fields__ if field != "timestamp" )
356
- mongo_field = new_collections .get (response_model .__name__ , main_field .lower ())
368
+ lookup_field = (
369
+ response_model .collection_name ()
370
+ if hasattr (response_model , "collection_name" )
371
+ else response_model .__name__
372
+ )
373
+ mongo_field = new_collections .get (lookup_field , main_field .lower ())
357
374
project_pipeline [main_field ] = f"${ mongo_field } "
358
375
logger .debug (f"Mapping { mongo_field } to { main_field } for model { response_model .__name__ } " )
359
376
0 commit comments