Skip to content

Instantly share code, notes, and snippets.

@luminoso
Created January 20, 2020 16:17
Show Gist options
  • Save luminoso/a32d56543ea4ee2cea983b5cde6b8357 to your computer and use it in GitHub Desktop.
Save luminoso/a32d56543ea4ee2cea983b5cde6b8357 to your computer and use it in GitHub Desktop.
quick connnect mongo pymongo
from pymongo import MongoClient
# pprint library is used to make the output look more pretty
from pprint import pprint
import urllib.parse
username = urllib.parse.quote_plus('xx')
password = urllib.parse.quote_plus('yy')
client = MongoClient(f'mongodb://{username}:{password}@172.31.8.226:27017/')
db=client.admin
# Issue the serverStatus command and print the results
serverStatusResult=db.command("serverStatus")
pprint(serverStatusResult)
cursor = client.list_databases()
for db in cursor:
print(db)
db = client["anDB"]
col = db["anCollection"]
x = col.find_one()
pprint(x)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment