1.安装pymongo
2.连接测试
#!/usr/bin/env python
# coding=utf-8
from pymongo import MongoClient
from datetime import datetime
#链接数据库的客户端
client = MongoClient('localhost', 27017)
# 选择数据库
db = client.test
# 选择集合
collection = db.blog
collection.insert({'title': 'python connect mongo', "content": "beautiful", "date": datetime.now()})
for obj in collection.find():
print (obj)
# collection.update({name: "xxx"}, {name: "XXX", args:...}) 第一个参数找到要要更新的文档, 后面是要更新的数据
# collection.remove()删除集合中的全部数据,但是集合依然存在,索引也在
# collection.drop()删除集合collection
3.测试结果
{'_id': ObjectId('5a7c0992620d01eb2e6d4761'), 'comments': [], 'content': 'lalalalal', 'title': 'test mongo', 'date': datetime.datetime(2018, 2, 8, 8, 20, 37, 995000)}
{'_id': ObjectId('5a7c5e7f620d01eb2e6d4763'), 'comments': [], 'content': 'lalalalal', 'title': 'test mongo', 'date': datetime.datetime(2018, 2, 8, 8, 20, 37, 995000)}
{'_id': ObjectId('5a7da8fcc133671dd4b3676b'), 'content': 'beautiful', 'title': 'python connect mongo', 'date': datetime.datetime(2018, 2, 9, 21, 58, 20, 777000)}