MongoDB Python API

安装
1
pip install pymongo
导入
1
from pymongo import MongoClient
连接MongoDB Server
1
client = MongoClient('localhost', 27017)
列出所有数据库
1
client.list_database_names()
创建/选择数据库

如果post_db不存在,则自动新建此数据库。

1
post_db = client.get_database('post_db')
列出库内所有的集合
1
post_db.list_collection_names()
新建/选择集合

如果post_collection不存在,则新建此集合。

1
post_collection = post_db.get_collection('post_collection')
插入一条文档
1
2
3
4
import datetime
post = {"author": 'zhangsan', 'text': '我的第一篇博客', "tags": ['mongodb', 'python', 'pymongo'],
"date": datetime.datetime.utcnow()}
post_collection.insert_one(post)
查询单条文档
1
post_collection.find_one()
插入多条文档
1
2
list = [{'author': 'lisi', "text": 'lisi text'}, {'author': 'wangwu', 'text': 'wangwu text'}]
post_collection.insert_many(list)
遍历集合内的文档
1
2
for i in post_collection.find():
print(i)