Home > Backend Development > Python Tutorial > How to connect and operate mongodb using python

How to connect and operate mongodb using python

高洛峰
Release: 2017-03-22 11:58:05
Original
2064 people have browsed it

Install the library file pymongo for python to connect to mongodb

 pip install pymongo
Copy after login

Python to connect to mongodb program

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)
Copy after login

Execute the program and get the following results:

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}
Copy after login

The above is the detailed content of How to connect and operate mongodb using python. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template