This article describes how to use python to connect to mongodb and how to install python to connect to the mongodb library file pymongo
Pip install pymongo
Connect to the mongodb program using python
Import pymongo conn = pymongo. consumer client ("ip", port) db = conn. admin # connect to the database. authenticate ("account", "password") # User Authentication db = conn. jwhdb. test. insert ({'id': 1, 'name': 'Kaka ', 'Sex': 'male'}) # insert a data = db. test. find () # print all data for I in data: print (I)
Run the program. The result is 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 ('58cb8140c720b222ec46f729'), 'id': 1}
The above describes how to connect to and operate mongodb using python. For more information, see other related articles in the first PHP community!