您的位置:首页 > 数据库 > Mongodb

python 操作三大主流数据库 第七课python操作MongoDB数据库

2020-01-12 17:16 441 查看

python 操作三大主流数据库 第七课

tags:

  • python3
  • 慕课网

categories:

  • python3
  • mysql
  • MongoDB

文章目录

  • 第二节 python操作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)
    • 点赞
    • 收藏
    • 分享
    • 文章举报
    道教儒佛电磁波 发布了36 篇原创文章 · 获赞 4 · 访问量 393 私信 关注
    内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
    标签: