Not sure how to change the new code to get things working as you want, but if your old query was working except for needing too long, and you have a pro or higher account, you could try using a background function to do the slow part without changing your code otherwise, potentially:
I can’t upload to pro a project that still does not generate revenue. Is a pitty that this static site does not let me deploy a express server. Not even that a request to save data in MongoDB cant be performed in less than ten seconds by netlify serverless fuctions.
Is It the solution change this cloud computing platform for someting else (Heroku, Vercel, CloudFlare)?