测开之路五十一:代码实现MongoDB增删改查
初始化时连接、析构时断开连接
from pymongo import MongoClient class Mogo(object): def __init__(self, host='127.0.0.1', port=27017):
""" 初始化时连接 """
self.connect = MongoClient(host, port) def __del__(self):
""" 析构时断开连接 """
self.connect.close()
插入:
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”的数据
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
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)
删除:
def delet(self, database, collection, filter):
""" 删:database: 数据库名、collection: 表名、filter: 查找条件 """
_database = self.connect.get_database(database) # 获取数据库对象
_collection = _database.get_collection(collection) # 获取表对象
_collection.delete_one(filter)
如:删除之前插入的数据
再刷新,表里面已经没有数据了
更新:
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})
最后增查改删的代码
from pymongo import MongoClient class Mogo(object): def __init__(self, host='127.0.0.1', port=27017):
""" 初始化时连接 """
self.connect = MongoClient(host, port) def __del__(self):
""" 析构时断开连接 """
self.connect.close() 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) 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) 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}) 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增删改查的更多相关文章
- springboot(十五):springboot+jpa+thymeleaf增删改查示例
这篇文章介绍如何使用jpa和thymeleaf做一个增删改查的示例. 先和大家聊聊我为什么喜欢写这种脚手架的项目,在我学习一门新技术的时候,总是想快速的搭建起一个demo来试试它的效果,越简单越容易上 ...
- spring boot(十五)spring boot+thymeleaf+jpa增删改查示例
快速上手 配置文件 pom包配置 pom包里面添加jpa和thymeleaf的相关包引用 <dependency> <groupId>org.springframework.b ...
- 测开之路五十:monggodb安装与初步使用
mongodb下载地址:https://www.mongodb.com/download-center Robo3T下载地址:https://robomongo.org/ 安装mongodb 双击无脑 ...
- 测开之路五十五:实现类似于unittest查找case
实现给一个路径,去查找test开头的测试用例文件 创建一个计算器的类,方便后面测试用 class Calculator(object): def add(self, x, y): return x + ...
- 测开之路六十一:接口测试平台之interface蓝图
create的js //添加header的函数function add_header() { // 这里是动态拼接html语句,带着样式,拼凑成页面的 "key [] value []&qu ...
- 测开之路五十六:实现类似unittest的断言
import inspect class Case(object): """ 实现断言 """ def __init__(self): se ...
- 测开之路五十三:unittest运行参数
Fixture:进行测试前的准备工作和测试后的清理操作.例如创建临时或是代理数据库,目录,服务进程等.用例(Case):最小的测试单元,检车特定输入的响应.TestCase作为所有用例的基类,测试ca ...
- 测开之路九十一:css常用的选择器
一:全局选择器:* 二:标签选择器,如给所有p标签加个背景色 三:id选择器:# ,如给id为id_01的元素加一个框 四:类选择器:. 如设置一个类选择器为blue,当有标签引用blue的时候,背景 ...
- 测开之路八十一:参数定义之*args和**kwargs
# *,不定长参数,*args# 定义函数参数def avg(score, *scores): return (score + sum(scores)) / (len(scores) + 1) ...
随机推荐
- 基于vux的Vue路由切换动画
const history = window.sessionStorage history.clear() let historyCount = history.getItem('count') * ...
- 读取交货单拣配数量PIKMG(转)
原文链接:https://www.591sap.com/thread-953-1-1.html SAP交货单交货数量在lips中直接读取,但是拣配数量lfimg,只存在vbfa中,且如果基本计量单位和 ...
- [Python3 练习] 010 找出藏在字符串中的“密码”
题目:找出藏在字符串中的"密码" (1) 描述 1) 题源 1 Python Challenge, level 3 2) 题源 2 小甲鱼老师的 Python 课程,第 20 讲课 ...
- nodejs安装失败
原文链接:https://www.cnblogs.com/huiziblog666/p/6274494.html 出现error 2502 和error2503是因为win8的权限问题所导致的,具体说 ...
- Linux服务正常启动,Linux服务器能访问,但是外部机器不能访问
公司用到了jenkins,就在自己虚拟机里面部署了一个jenkins.部署成功之后,在Linux虚拟机里面能正常访问,但是外部真实机却不能访问.当时的第一反应就是觉得应该是权限问题,猜测会不会是jen ...
- Codeforces 1061C (DP+滚动数组)
题面 传送门 分析 考虑DP 设\(dp[i][j]\)表示前i个数选出的序列长度为j的方案数 状态转移方程为: \[ dp[i][j]= \begin{cases}dp\left[ i-1\righ ...
- flask项目中使用富文本编辑器
flask是一个用python编写的轻量级web框架,基于Werkzeug WSGI(WSGI: python的服务器网关接口)工具箱和Jinja2模板,因为它使用简单的核心,用extension增加 ...
- 在Eclipse的kepler中执行OSGIproject出错的解决方式
版权声明:本文为博主原创文章,未经博主同意不得转载. https://blog.csdn.net/canlets/article/details/29620301 今天学习OSGI的过程中依照书上所述 ...
- SQL server 连接 查询
在sql server中,我们经常能用到连接,今天总结一下连接的基础知识.连接的分类: 交叉连接CROSS JOIN 内连接INNER JOIN 外连接{左外连接LEFT [OUTER] JOIN : ...
- day64--pymysql模块的使用、视图、触发器、函数、存储过程、事务
一.pymysql的下载和使用 (一)pymysql模块的下载:pip3 install pymysql # 实现:使用Python实现用户登录,如果用户存在则登录成功(假设该用户已在数据库中) im ...