您的位置:首页 > 编程语言 > Python开发

使用Python操作mongo

2017-10-19 15:22 399 查看
# coding:utf-8
from pymongo import MongoClient
client = MongoClient('localhost', 27017)

# 获得一个数据库链接
db = client.weixin

# 从数据库中获得一个集合
article = db.article

# 向集合中插入数据
item = {'content': '2.很好的人'}
article.insert_one(item)

# 更新数据
result = article.update_one({'content': '1.hello'}, {'$set': {'content': '2.hello'}})
result = article.update_many({'content': '2.hello'}, {'$set': {'content': '1.hello'}})
print result.matched_count

# 删除数据
article.delete_one({'content': '2.hello'})
result = article.delete_many({'content': '1.hello'})
print result.deleted_count

# 查询所有数据
for item in article.find():
print item['content']
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  python 数据库