MongoDB Note5:Pymongo

MongoDB Note5:Pymongo

0. 官网

1. Pymongo安装

pip install pymongo

2. 连接

from pymongo import MongoClient host = "192.168.92.145" port = 27017 user_name = "demodb" user_pwd = "password" # collection name db_name = "demo" coll_name = 'test' mongo_client = MongoClient(host, port) db = mongo_client[db_name] # 认证 db.authenticate(user_name, user_pwd) # 需要用的的表需要在认证后用client单独去关联 mongo_db = mongo_client[db_name] mongo_coll = mongo_db[coll_name] # 获取所有collections并打印,验证是否登录成功 coll_names = mongo_db.list_collection_names(session=None) print(coll_names)