Hey @Musa_Karim - welcome to the MongoDB community forums!

PyMongo is a really lazy driver, so it won’t connect to the database cluster until it absolutely has to. This means the following things don’t do anything on the network:

  • Creating a MongoClient
  • Getting a database
  • Getting a collection
  • Running find or aggregate :scream: (these return cursors, which are empty until you start to use them)

The following things do connect to the cluster and do stuff:

  • Running an insert or update
  • Running find_one
  • Looping through the results of a find or aggregate call.

So … what this means is that your code isn’t able to find the database and collection - it’s not even trying! This trips a lot of people up.

The error message states that your MongoClient is trying to connect to “localhost:27017”, which is the default if no connection string is provided. This means that you could be calling MongoClient() with no arguments, or what I suspect you’re doing is calling MongoClient(MDB_URI) where MDB_URI is None for some reason.

I hope this helps!

Mark