测开之路五十一:代码实现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) ...
随机推荐
- IDF-CTF-图片里的英语 writeup
题目链接:http://ctf.idf.cn/index.php?g=game&m=article&a=index&id=34 一恒河沙中有三千世界,一张图里也可以有很多东西. ...
- 什么是HTTP协议?常用的状态码有哪些?
一.HTTP简介 HTTP协议是Hyper Text Transfer Protocol(超文本传输协议)的缩写,是用于从万维网(WWW:World Wide Web )服务器传输超文本到本地浏览器的 ...
- jvm性能监控(4)–JVM的监控工具Jconsole
下面主要说一下怎么JConsole远程连接springboot 项目 java \-Djava.rmi.server.hostname=192.131.149.42 \-Dcom.sun.manag ...
- JVM(2)之 JAVA堆
开发十年,就只剩下这套架构体系了! >>> 之前我们说到了栈,它在内存中是连续的空间:保存一个个的栈帧,对应一次次方法的调用:还讲到了他是保存对象的引用,那么对象存在哪里呢?我们 ...
- Linux知识-不断更新2
为了自己看的更清楚,也为了不断的总结,每次更新后都会另发一篇. 工作中遇到某一文件夹磁盘空间不够,当然每次都是清理日志,最后发现还是不太行,还不能扩容,只能先想办法迁移目录,避免此问题发生,但在这之前 ...
- ed-tue-robotics
https://github.com/tue-robotics/ed ubuntu16.04 安装libsdformat4-dev ,libsdformat4 1./usr/include/sdfor ...
- 微信小程序(4)--二维码窗口
微信小程序二维码窗口: <view class="btn" bindtap="powerDrawer" data-statu="open&quo ...
- css3 清除浮动
eg:三个div,父级div下面有两个div分别float:left和float:right <style> .container{width:400px;border:3px soild ...
- python关于window文件写入后,换行默认\r\n的问题
因为python兼容各种平台,所以当在window打开文本文件写入后,换行会默认写成\r\n linux是\n 如果想去掉换行的\r 解决方法:在open函数里写入换行要求即可 with open(f ...
- Delphi 运行后错误提示“无效的授权说明”
Delphi 运行后错误提示“无效的授权说明” 一般情况是:数据库的连接出现了问题. 解决方法:检查加载数据库是否正常,能否正常连接.