Connecting to atlas

Having an issue connecting to Altas Cluster, I have whitelisted ip 0.0.0.0. To ensure I was able to connect to the db I printed the collection name to console. But when I try and insert a document into the collection, I get this error.

pymongo.errors.ServerSelectionTimeoutError: mongastreamlistener-ja2vb.mongodb.net:27017 : [Errno 8] nodename nor servname provided, or not known

I have tried
db.collection_name.insert_one(json_data)
collection_name.insert_one(json_data) same error.

Code:
from pymongo import MongoClient

try:
conn = MongoClient()
print(“Connected successfully!!!”)
except:
print(“Could not connect to MongoDB”)

database

db = conn.my_database

Created or Switched to collection names

collection = db.my_collection

emp_rec1 = {
“name”:“Mr.Geek”,
“eid”:24,
“location”:“delhi”
}
emp_rec2 = {
“name”:“Mr.Shaurya”,
“eid”:14,
“location”:“delhi”
}

Insert Data

rec_id1 = collection.insert_one(emp_rec1)
rec_id2 = collection.insert_one(emp_rec2)

Please keep you question in one thread. The discussion has started in Connect to Atlas with Pymongo and you should close this thread.

1 Like