python 操作三大主流数据库 第七课python操作MongoDB数据库
2020-01-12 17:16
441 查看
python 操作三大主流数据库 第七课
tags:
- python3
- 慕课网
categories:
- python3
- mysql
- MongoDB
文章目录
第一节 python连接数据库MongoDB
1. pymongo安装
1. 安装:pip install -i https://pypi.douban.com/simple/ pymongo 2. 文档: http://api.mongodb.com/ python/current/ 3. pypi: https://pypi.python.org/pypi/pymongo 4. github: https://github.com/mongodb/mongo-python-driver/
2. 连接MongoDB数据库
1. 首先把MongoDB的服务启动起来mongod.exe --dbpath D:\MongoDB\DB 2. 方式一:简写client = MongoClient() 3. 方式二:指定端口和地址client = MongoClient('localhost', 27017) 4. 方式三:使用URI client = MongoClient('mongodb://localhost:27017/)
from pymongo import MongoClient client = MongoClient() #client = MongoClient('localhost', 27017) #client = MongoClient('mongodb://localhost:27017/) # 可以查看client的一些信息 client.HOST client.PORT client.database_names() client.close() # 这里会自动在链接上,因为MongoDB自带了一个连接池。pymongo中自带 # 切换数据库 db = client.blog db = client["blog-xxxx"] dir(db)
第二节 python操作MongoDB增删改查
1. 新增数据
# python 直接对MongDB数据库操作 from pymongo import MongoClient from datetime import datetime class TestMongo: def __init__(self): self.client = MongoClient() self.db = self.client['blog'] def add_one(self): # 新增一条数据 post = { 'title': '标题', 'content': ' 内容', 'created at': datetime .now(), } rest = self.db.blog.posts.insert_one(post) return rest def main(): obj = TestMongo() rest = obj.add_one() print(rest.inserted_id) if __name__=='__main__': main()
2. 查询数据
def get_one(self): '''查询一条数据''' return self.db.blog.posts.find_one() def get_more(self): '''查询多条数据''' return self.db.blog.posts.find() def get_from_old(self, oid): '''根据记录的ID来获取数据''' from bson.objectid import ObjectId # MongDB 中不能直接传字符串, 需要传一个ObjectId对象 return self.db.blog.posts.find_one({'_id': ObjectId(oid)}) def main(): obj = TestMongo() #rest = obj.add_one() rest = obj.get_one() # type(rest) 实际上是一个字典 print(rest['_id']) rest = obj.get_more() for item in rest: print(item['_id'])
3. 修改数据
def update(self): '''修改数据''' # 修改一条数据 x 增加三,如果没有x设置为三 rest = self.db.blog.posts.update_one({'title': '标题'}, {'$inc': {'x': 3}}) print(rest.matched_count) print(rest.modified_count) # 修改多条数据 rest = self.db.blog.posts.update_many({}, {'$inc': {'x': 1}}) print(rest.matched_count) print(rest.modified_count)
4. 删除数据
def delete(self): # 删除一条数据 rest = self.db.blog.posts.delete_one({'title': '标题'}) print(rest.deleted_count) # 删除多条数据 rest = self.db.blog.posts.delete_many({'title': '标题'}) print(rest.deleted_count)
- 点赞
- 收藏
- 分享
- 文章举报
相关文章推荐
- 最新Python操作三大主流数据库分享
- 最新Python操作三大主流数据库教程 MySQL/MongoDB/Redis
- python操作三大主流数据库(1)python操作mysql①windows环境中安装python操作mysql数据库的MySQLdb模块mysql-client
- python操作三大主流数据库(3)python操作mysql③python操作mysql的orm工具sqlaichemy安装配置和使用
- python 操作三大主流数据库 第九课mongoDB实战
- python 操作三大主流数据库 第十一课 Redis数据库实战网易新闻
- python 操作三大主流数据库 第十课Redis原生操作
- python 操作三大主流数据库 第八课 ODM模型操作MongoDB
- Python操作三大主流数据库
- python操作三大主流数据库(2)python操作mysql②python对mysql进行简单的增删改查
- 第七课 Python3 简单三步学会数据库操作
- Python操作MySQL+MongoDB+Redis三大主流数据库
- python操作三大数据库(二)--------mongodb
- Python开发工程师 Python操作三大数据库
- java对三大主流数据库sql server、mysql、oracle的连接和操作
- 三大主流数据库sql server、mysql、oracle的分页操作
- Python操作主流数据库
- python中连接三大主流数据库mysql,mongodb,redis
- Python操作三大数据库
- python操作三大数据库(一)---------------mysql