初始化时连接、析构时断开连接

  1. from pymongo import MongoClient
  2.  
  3. class Mogo(object):
  4.  
  5. def __init__(self, host='127.0.0.1', port=27017):
    """ 初始化时连接 """
    self.connect = MongoClient(host, port)
  6.  
  7. def __del__(self):
    """ 析构时断开连接 """
    self.connect.close()

插入:

  1. def insert(self, database, collection, documents):
    """ 增:database: 数据库名、collection: 表名、documents: 数据 """
    _database = self.connect.get_database(database) # 获取数据库对象
    _collection = _database.get_collection(collection) # 获取表对象
    return _collection.insert_one(documents)

准备好几条数据

查找

如:查name为“XXX”的数据

  1. def search(self, database, collection, filter):
    """ 查:database: 数据库名、collection: 表名、filter: 查找条件 """
    _database = self.connect.get_database(database) # 获取数据库对象
    _collection = _database.get_collection(collection) # 获取表对象
    return _collection.find(filter)

默认查出来是结果集

结果集可以转list,也可以用for循环取,取出来一个是list,一个是字典

增加过滤条件:使用projection

如:查sex=“男”的数据,且不返回id和sex

  1. def search(self, database, collection, filter):
    """ 查:database: 数据库名、collection: 表名、filter: 查找条件 """
    projection = None
    if "projection" in filter:
    projection = filter.pop("projection")
    _database = self.connect.get_database(database) # 获取数据库对象
    _collection = _database.get_collection(collection) # 获取表对象
    return _collection.find(filter, projection)

删除:

  1. def delet(self, database, collection, filter):
    """ 删:database: 数据库名、collection: 表名、filter: 查找条件 """
    _database = self.connect.get_database(database) # 获取数据库对象
    _collection = _database.get_collection(collection) # 获取表对象
    _collection.delete_one(filter)

如:删除之前插入的数据

再刷新,表里面已经没有数据了

更新:

  1. def update(self, database, collection, filter, documents):
    """ 改:database: 数据库名、collection: 表名、filter: 查找条件、documents: 数据 """
    _database = self.connect.get_database(database) # 获取数据库对象
    _collection = _database.get_collection(collection) # 获取表对象
    _collection.update_one(filter, {'$set': documents})

最后增查改删的代码

  1. from pymongo import MongoClient
  2.  
  3. class Mogo(object):
  4.  
  5. def __init__(self, host='127.0.0.1', port=27017):
    """ 初始化时连接 """
    self.connect = MongoClient(host, port)
  6.  
  7. def __del__(self):
    """ 析构时断开连接 """
    self.connect.close()
  8.  
  9. def insert(self, database, collection, documents):
    """ 增:database: 数据库名、collection: 表名、documents: 数据 """
    _database = self.connect.get_database(database) # 获取数据库对象
    _collection = _database.get_collection(collection) # 获取表对象
    return _collection.insert_one(documents)
  10.  
  11. def search(self, database, collection, filter):
    """ 查:database: 数据库名、collection: 表名、filter: 查找条件 """
    projection = None
    if "projection" in filter:
    projection = filter.pop("projection")
    _database = self.connect.get_database(database) # 获取数据库对象
    _collection = _database.get_collection(collection) # 获取表对象
    return _collection.find(filter, projection)
  12.  
  13. def update(self, database, collection, filter, documents):
    """ 改:database: 数据库名、collection: 表名、filter: 查找条件、documents: 数据 """
    _database = self.connect.get_database(database) # 获取数据库对象
    _collection = _database.get_collection(collection) # 获取表对象
    _collection.update_one(filter, {'$set': documents})
  14.  
  15. def delet(self, database, collection, filter):
    """ 删:database: 数据库名、collection: 表名、filter: 查找条件 """
    _database = self.connect.get_database(database) # 获取数据库对象
    _collection = _database.get_collection(collection) # 获取表对象
    _collection.delete_one(filter)

测开之路五十一:代码实现MongoDB增删改查的更多相关文章

  1. springboot(十五):springboot+jpa+thymeleaf增删改查示例

    这篇文章介绍如何使用jpa和thymeleaf做一个增删改查的示例. 先和大家聊聊我为什么喜欢写这种脚手架的项目,在我学习一门新技术的时候,总是想快速的搭建起一个demo来试试它的效果,越简单越容易上 ...

  2. spring boot(十五)spring boot+thymeleaf+jpa增删改查示例

    快速上手 配置文件 pom包配置 pom包里面添加jpa和thymeleaf的相关包引用 <dependency> <groupId>org.springframework.b ...

  3. 测开之路五十:monggodb安装与初步使用

    mongodb下载地址:https://www.mongodb.com/download-center Robo3T下载地址:https://robomongo.org/ 安装mongodb 双击无脑 ...

  4. 测开之路五十五:实现类似于unittest查找case

    实现给一个路径,去查找test开头的测试用例文件 创建一个计算器的类,方便后面测试用 class Calculator(object): def add(self, x, y): return x + ...

  5. 测开之路六十一:接口测试平台之interface蓝图

    create的js //添加header的函数function add_header() { // 这里是动态拼接html语句,带着样式,拼凑成页面的 "key [] value []&qu ...

  6. 测开之路五十六:实现类似unittest的断言

    import inspect class Case(object): """ 实现断言 """ def __init__(self): se ...

  7. 测开之路五十三:unittest运行参数

    Fixture:进行测试前的准备工作和测试后的清理操作.例如创建临时或是代理数据库,目录,服务进程等.用例(Case):最小的测试单元,检车特定输入的响应.TestCase作为所有用例的基类,测试ca ...

  8. 测开之路九十一:css常用的选择器

    一:全局选择器:* 二:标签选择器,如给所有p标签加个背景色 三:id选择器:# ,如给id为id_01的元素加一个框 四:类选择器:. 如设置一个类选择器为blue,当有标签引用blue的时候,背景 ...

  9. 测开之路八十一:参数定义之*args和**kwargs

    # *,不定长参数,*args# 定义函数参数def avg(score, *scores):    return (score + sum(scores)) / (len(scores) + 1) ...

随机推荐

  1. Show Me the Code

    最近在练习写Python代码,拥有150多道程序员面试题的LeetCode注重算法的实现,锻炼思维,还能在线测试代码的正确性,而Python练习册涉及到了Python实际的应用,锻炼解决问题的能力,托 ...

  2. PHP学习:set_time_limit,max_execution_time,sleep

    set_time_limit 设置脚本最大允许执行时间,可以在php脚本中使用, 参数为秒,如果为0,表示无时间限制: set_time_limit(seconds); max_execution_t ...

  3. memset 初始化数组 & 实现原理

    初始化数组可不必使用n重for循环. 原理 memset具有初始化数组的功能,能够初始化数组中的每一个值. 它是将数组中的每一个数的二进制的每一个字节初始化的. 比如初始化int类型的a数组:mems ...

  4. Linux mysql 乱码

    http://www.pc6.com/infoview/Article_63586.html http://itindex.net/detail/41748-linux-mysql-5.5 http: ...

  5. 全文搜索引擎 elasticsearch.net

    原文:https://www.cnblogs.com/lonelyxmas/p/10767436.html

  6. linux网络子系统调优

  7. 一、C#获取特性(坑)

    一.C#获取特性(坑) [Description("系统状态码")] [Display(Name = "成功", Description = "请求成 ...

  8. zabbix入门之添加监控项

    zabbix入门之添加监控项 添加一个不带参数的监控项(system.cpu.switches) 进入"配置"-->"主机"选择某主机的"监控项 ...

  9. 自己关于SSM框架的搭建

    第一步 导入相应的包 spring springmvc 需要的包 spring-webmvc spring-aop spring-beans apring-context spring-core sp ...

  10. nutz包的学习

    参考资料: 1.http://www.nutzam.com/core/nutz_preface.html