安装python连接mongodb的库文件pymongo
pip install pymongo
python连接mongodb程序
import pymongo conn = pymongo.MongoClient("ip",端口) db = conn.admin #连接库 db.authenticate("账号","密码") #用户认证 db=conn.jwh db.test.insert({‘id‘:1,‘name‘:‘kaka‘,‘sex‘:‘male‘}) #插入一个数据 data=db.test.find() #打印所有数据 for i in data: print(i)
执行程序,得到结果如下:
C:\Users\Administrator\AppData\Local\Programs\Python\Python35-32\python.exe D:/Users/Administrator/PycharmProjects/untitled/test/conn_mongodb.py {‘1‘: 1.0, ‘_id‘: ObjectId(‘58c13ed1b5ad0ac03e6cbb80‘)} {‘sex‘: ‘male‘, ‘name‘: ‘kaka‘, ‘_id‘: ObjectId(‘58cb7dd7c720b21eb011db7d‘), ‘id‘: 1} {‘sex‘: ‘male‘, ‘name‘: ‘kaka‘, ‘_id‘: ObjectId(‘58cb8140c720b222ec46f729‘), ‘id‘: 1}
时间: 2024-11-04 01:38:23