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

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增删改查的更多相关文章

  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. 微信小程序这一块(续)

    1.设置头部的信息 通过wx.setNavigationBarTitle 详情见:https://developers.weixin.qq.com/miniprogram/dev/api/ui/nav ...

  2. Go错误处理机制及自定义错误

    错误处理机制: 先看一段代码:看看输出什么? package mainimport "fmt" func test() { num1 := 10 num2 := 0 res := ...

  3. 监控软件之open-falcon

    一.open-falcon介绍 1)中文社区介绍 http://book.open-falcon.org/zh_0_2/intro/ 参照文档: https://www.cnblogs.com/LAl ...

  4. K3 cloud选单时候必须把必录的数据录完以后才可以选单

    解决办法:在bos中把选单按钮的提交时候校验打勾

  5. 卷积神经网络CNN原理以及TensorFlow实现

    在知乎上看到一段介绍卷积神经网络的文章,感觉讲的特别直观明了,我整理了一下.首先介绍原理部分. [透析] 卷积神经网络CNN究竟是怎样一步一步工作的? 通过一个图像分类问题介绍卷积神经网络是如何工作的 ...

  6. 在Ubuntu上安装LAMP(Apache、Mysql、Php)

    原文地址:https://howtoubuntu.org/how-to-install-lamp-on-ubuntu Ubuntu有很多工具可以帮助我们一键配置LAMP环境,比如tasksel,但这些 ...

  7. ansible笔记(三)--模块讲解

    ansible 常用命令 ansible-doc ansible-playbook ansible-vault ansible-console ansible-galaxy ansible-pull ...

  8. [BZOJ3626] [LNOI2014] LCA 离线 树链剖分

    题面 考虑到询问的\(l..r,z\)具有可减性,考虑把询问差分掉,拆成\(r,z\)和\(l-1,z\). 显然这些LCA一定在\(z\)到根的路径上.下面的问题就是怎么统计. 考虑不是那么暴力的暴 ...

  9. Maven项目的pom.xml配置文件格式初识

    Maven项目 有pom.xml文件的项目就已经是一个maven项目了,但是还没有被maven托管,我们需要将该项目添加为maven项目 <project xmlns="http:// ...

  10. 07.整合jsp、整合freemarker、整合thymeleaf

    整合jsp pom.xml部分内容 <packaging>war</packaging> </dependencies> <dependency> &l ...