一.連接數(shù)據(jù)庫
def connect(self):
????while True:
? ? ? ? ? ? ?client = pm.MongoClient("數(shù)據(jù)庫地址", serverSelectionTimeoutMS=3)
? ? ? ? ? ? ? try: client.admin.command("ping")
? ? ? ? ? ? ? except (ConnectionError) as e:
? ? ? ? ? ? ? ? ? ? ? ?return None
? ? ? ? ? ? ? ?else:
? ? ? ? ? ? ? ? ? ? ? ? break
? ? ? ?return client
二.獲取數(shù)據(jù)表
db = client.test? #數(shù)據(jù)庫
stb = db.users #用戶表
三.查詢
users= stb.find()
查詢文檔:?http://www.runoob.com/mongodb/mongodb-query.html
四.遍歷
for item in users:
? ? print(item)