Install Python connection to MongoDB library file Pymongo
Pip Install Pymongo
Python Connection MongoDB Program
Import Pymongoconn = Pymongo. Mongoclient ("IP", port) db = Conn.admin #连接库db. Authenticate ("account", "password") #用户认证db =conn.jwhdb.test.insert ({' id ': 1, ' name ': ' Kaka ', ' sex ': ' Male '}) #插入一个数据data =db.test.find () #打印所有数据for i in Data:print (i)
Execute the procedure and get the result as follows:
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 (' 5 8cb8140c720b222ec46f729 '), ' id ': 1}
Python connects MongoDB and operates