I am trying to connect to a mongo database hosted on an AWS server using pymongo on the PyCharm IDE. The PyCharm project is connected to the server fine through SSH. My problem is getting the database contents into my Python script for data analysis.
I can connect to the server and run my PyCharm project code on it fine. I'm just struggling to properly connect to the database that is on the same server. Heres what I've been trying with no success:
from pymongo import MongoClient
import pprint
mongo_client = MongoClient('mongodb://username:password@localhost:27017/test')
data_base = mongo_client.test_database
print(pprint.pprint(data_base.list_collection_names()))
collection = data_base.test_collection.find().limit(10)
print(pprint.pprint(collection))
I do not get errors as such, after executing the above, but the output suggests I've not connected to any database and retrieved any data, i.e.:
[]
None
<pymongo.cursor.Cursor object at 0x7ff4b538eed0>
None
I know the data is there because I can see it when I connect to the server through the terminal, start the database and run commands like show dbs;
and show collections;
then get data out of a collection using db.collection.find()
.
Any ideas? I have a feeling that I am not setting things up correctly but I'm also paranoid that I haven't been given the correct access rights/information to successfully connect.
Thanks in advance!