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 (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://")

with client:
    db = client.cloud_db

and output is not found:

MongoClient(host=[''], document_class=dict, tz_aware=False, connect=True)
Traceback (most recent call last):
  File "C:\Users\chuan\OneDrive\Desktop\10.13_connect_mongoDB\", line 11, in <module>
  File "C:\Users\chuan\AppData\Local\Programs\Python\Python310\lib\site-packages\pymongo\", 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,

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://")

#name my DB name

# 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\", 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\", 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\", 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\", 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\", line 105, in csot_wrapper
    return func(self, *args, **kwargs)
  File "C:\Users\chuan\AppData\Local\Programs\Python\Python310\lib\site-packages\pymongo\", 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\", line 1229, in _select_server
    server = topology.select_server(server_selector)
  File "C:\Users\chuan\AppData\Local\Programs\Python\Python310\lib\site-packages\pymongo\", 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\", 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\", 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\", 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.