I am creating a Python-based program and wanted to store data in MongoDB Atlas but when I created a cluster and tried connecting it using a Module named pymongo, which is used to access MongoDB Atlas. It didn’t connect and gave this error :
Traceback (most recent call last):
~Something here~
dns.resolver.NoNameservers: All nameservers failed to answer the query _mongodb._tcp.clusterai.7zs4x3d.mongodb.net. IN SRV:
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
~Something here again~
pymongo.errors.ConfigurationError: All nameservers failed to answer the query _mongodb._tcp.clusterai.7zs4x3d.mongodb.net. IN SRV:
My Code was this and all credentials were correct! :
import pymongo
connection_pass = "MyPass"
connection_string = f"mongodb+srv://ansh-admin:{connection_pass}@clusterai.7zs4x3d.mongodb.net/test"
if __name__ == "__main__":
client = pymongo.MongoClient(connection_string)
When I tried removing +srv from the connection string as ChatGPT asked me to do. It gave me no error with this code :
import pymongo
connection_pass = "MyPass"
connection_string = f"mongodb://ansh-admin:{connection_pass}@clusterai.7zs4x3d.mongodb.net/test"
if __name__ == "__main__":
client = pymongo.MongoClient(connection_string)
db = client['test-database']
collection = db['test-col']
but when I tried writing data it gave me an error. My code was :
import pymongo
connection_pass = "MyPass"
connection_string = f"mongodb://ansh-admin:{connection_pass}@clusterai.7zs4x3d.mongodb.net/test"
if __name__ == "__main__":
client = pymongo.MongoClient(connection_string)
db = client['test-database']
collection = db['test-col']
docs = {"name" : "test"}
data_id = collection.insert_one(docs).inserted_id
print(data_id)
The error it gave was :
Traceback (most recent call last):
~something here~
pymongo.errors.ServerSelectionTimeoutError: clusterai.7zs4x3d.mongodb.net:27017: [Errno 11001] getaddrinfo failed, Timeout: 30s, Topology Description: <TopologyDescription id: 640f56ba89e1802b2f068d6a, topology_type: Unknown, servers: [<ServerDescription ('clusterai.7zs4x3d.mongodb.net', 27017) server_type: Unknown, rtt: None, error=AutoReconnect('clusterai.7zs4x3d.mongodb.net:27017: [Errno 11001] getaddrinfo failed')>]>
Help me out, please! Thanks in advance!