I'm a beginner of Python and MongoDB but I have tried your code and it works perfecty on local MongoDB.
If i try to access to the same database on Atlas Cloud and in tis case occurrs the error "SSL Certificate invalid".
I've tried but i don't know how to resolve it.