I'm encountering an issue with a Python script that utilizes mongoengine to query MongoDB.
When executing the same query multiple times, it returns the expected data some of the times, while other times it returns no data at all from the collection.
Sample code:
class Employee(Document):
_id = ObjectIdField()
department: str = StringField()
employeeName: str = StringField()
createdAt: int = LongField()
meta = {
'strict': False,
'collection': 'employee'
}
if __name__ == "__main__":
unique_names = Employee.objects(group="foo").distinct("employeeName")
print(len(unique_names))
cursor1 = Employee.objects(employeeName="john")
print(len(list(cursor1)))
With the data being unchanged in the DB, sometimes the 2 queries are returning empty data, as if there are no records in Employee collection.
I could not see any pattern as to when it is failing. Out of 100 invocations, it sometimes fails 2-3 times where as somtimes it fails 70-80% of the time!
Please note -
- This scrip is running in AWS Lambda + APIGateway setup.
- MongoDB version - 5.0.25
- mongoengine - 0.24.2
- Python 3.9