How to read and write from existing mongoDB by IP address?

How can I read and write from an existing mongoDB by IP address?

I learn from here (pymongo): Connect to MongoDB from Python Application

Saying that technically using (Windows + pymongo + python script) can read and write to mongoDB.
I have IP address and existing mongoDB(already have company’s collection/table),

environment: Windows 11 cmd , python

I should make more clear that I have actual mongoDB collection/table with IP address 196.xxx.xxx.xxx (x is for actual ip address )

  • tried .py code for read and write from an existing mongoDB by IP address
from pymongo import MongoClient
import pymongo
import pandas as pd

client =pymongo.MongoClient("mongodb://196.xxx.xxx.xxx:27017")
print(client)

with client:
    
    db = client.cloud_db
    print(db.collection_names())

and output is not found:

C:\Users\chuan\OneDrive\Desktop\10.13_connect_mongoDB>python 1.py
MongoClient(host=['196.xxx.xxx.xxx:27017'], document_class=dict, tz_aware=False, connect=True)
Traceback (most recent call last):
  File "C:\Users\chuan\OneDrive\Desktop\10.13_connect_mongoDB\1.py", line 11, in <module>
    print(db.collection_names())
  File "C:\Users\chuan\AppData\Local\Programs\Python\Python310\lib\site-packages\pymongo\collection.py", line 3194, in __call__
    raise TypeError(
TypeError: 'Collection' object is not callable. If you meant to call the 'collection_names' method on a 'Database' object it is failing because no such method exists.

online image link:(about the actual mongoDB I want to connect, print,
write)

if I want to list up all the collections/tables from cloud_db (in the mongoDB picture above) and maybe read and write to cloud_db's one of the collection named eform_detail , then what should I do??

here is my tried code after using client.list_database_names()

import pymongo

#connect to my desire mongoDB by IP address
client = pymongo.MongoClient("mongodb://196.xxx.xxx.xxx:27017")

#name my DB name
mydb_Name="cloud_db"

# set mydb will have all properties from myclient[mydb_Name]
mydb = client[mydb_Name]

#print all DB name
print("now exsisting DB are:  ",client.list_database_names())

#if DB exsist, then print it out
# and the "cloud_db" already exsist 
dblist = client.list_database_names()
if mydb_Name in dblist:
  print("Your DB: {0}exsist!!!!".format(mydb_Name))

and error raise:

Traceback (most recent call last):
  File "C:\Users\chuan\OneDrive\Desktop\10.13_connect_mongoDB\2.py", line 13, in <module>
    print("now exsisting DB are:  ",client.list_database_names())
  File "C:\Users\chuan\AppData\Local\Programs\Python\Python310\lib\site-packages\pymongo\mongo_client.py", line 1839, in list_database_names
    return [doc["name"] for doc in self.list_databases(session, nameOnly=True, comment=comment)]
  File "C:\Users\chuan\AppData\Local\Programs\Python\Python310\lib\site-packages\pymongo\mongo_client.py", line 1812, in list_databases
    res = admin._retryable_read_command(cmd, session=session)
  File "C:\Users\chuan\AppData\Local\Programs\Python\Python310\lib\site-packages\pymongo\database.py", line 843, in _retryable_read_command
    return self.__client._retryable_read(_cmd, read_preference, session)
  File "C:\Users\chuan\AppData\Local\Programs\Python\Python310\lib\site-packages\pymongo\_csot.py", line 105, in csot_wrapper
    return func(self, *args, **kwargs)
  File "C:\Users\chuan\AppData\Local\Programs\Python\Python310\lib\site-packages\pymongo\mongo_client.py", line 1413, in _retryable_read
    server = self._select_server(read_pref, session, address=address)
  File "C:\Users\chuan\AppData\Local\Programs\Python\Python310\lib\site-packages\pymongo\mongo_client.py", line 1229, in _select_server
    server = topology.select_server(server_selector)
  File "C:\Users\chuan\AppData\Local\Programs\Python\Python310\lib\site-packages\pymongo\topology.py", line 272, in select_server
    server = self._select_server(selector, server_selection_timeout, address)
  File "C:\Users\chuan\AppData\Local\Programs\Python\Python310\lib\site-packages\pymongo\topology.py", line 261, in _select_server
    servers = self.select_servers(selector, server_selection_timeout, address)
  File "C:\Users\chuan\AppData\Local\Programs\Python\Python310\lib\site-packages\pymongo\topology.py", line 223, in select_servers
    server_descriptions = self._select_servers_loop(selector, server_timeout, address)
  File "C:\Users\chuan\AppData\Local\Programs\Python\Python310\lib\site-packages\pymongo\topology.py", line 238, in _select_servers_loop
    raise ServerSelectionTimeoutError(
pymongo.errors.ServerSelectionTimeoutError: Could not reach any servers in [('mongodb', 27017)]. Replica set is configured with internal hostnames or IPs?, Timeout: 30s, Topology Description: <TopologyDescription id: 6347a91a4c61799b7b1594ac, topology_type: ReplicaSetNoPrimary, servers: [<ServerDescription ('mongodb', 27017) server_type: Unknown, rtt: None, error=AutoReconnect('mongodb:27017: [Errno 11001] getaddrinfo failed')>]>

my expect output is : (then I could do read and write later)
mongoDB pic : 500 02 — Postimages

now exsisting DB are: [ ‘admin’ , ‘cloud_db’]

inside cloud_db are: [ ‘asset’ , ‘bas_bom_plain’ , ‘bas_part_info’ ,
‘bom’ , ‘comments’ , ‘def’ , ‘def_detail’ , ‘doc’ , ‘eform’,
‘eform_detail’ …etc(the collections in picture) ]

Your host is a single node replicaSet. The hostname configured in the replicaSet is not resolvable by your client.

When the driver is connecting it retrieves the topology of the replicaSet so it can connect correctly.

You can either:

  • fix the name resolution
  • change the replicaset to a resolveable name
  • use the directConnection=true option (mongo uri or mongoClient keyword argument)
1 Like

This topic was automatically closed 5 days after the last reply. New replies are no longer allowed.