测开之路五十一:代码实现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)

原文地址:https://www.cnblogs.com/zhongyehai/p/10976943.html

时间: 2024-08-26 21:31:53

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

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

这篇文章介绍如何使用jpa和thymeleaf做一个增删改查的示例. 先和大家聊聊我为什么喜欢写这种脚手架的项目,在我学习一门新技术的时候,总是想快速的搭建起一个demo来试试它的效果,越简单越容易上手最好.在网上找相关资料的时候总是很麻烦,有的文章写的挺不错的但是没有源代码,有的有源代码但是文章介绍又不是很清楚,所在找资料的时候稍微有点费劲.因此在我学习Spring Boot的时候,会写一些最简单基本的示例项目,一方面方便其它朋友以最快的方式去了解,一方面如果我的项目需要用到相关技术的时候,直

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

快速上手 配置文件 pom包配置 pom包里面添加jpa和thymeleaf的相关包引用 <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.

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

import inspect class Case(object): """ 实现断言 """ def __init__(self): self.result = {} # 存断言的结果 def _assert(self, expression, message): """ 真正执行断言的函数 """ """ [2][1]二维数组,以a调b时b调c为例 第一

测开之路五十二:蓝图的用法

目录结构 html <!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8"> <title>蓝图渲染</title></head><body><h1>这里是蓝图渲染</h1></body></html> 子app(创建不同的蓝图,如接口测试.ui测试.性能测试)

ABP学习入门系列(五)(展示实现增删改查)

大致要实现的 效果如下 1,添加Controller(用到的X.PagedList 注意到nuget添加) using System.Web.Mvc; using Abp.Application.Services.Dto; using Abp.Runtime.Caching; using Abp.Threading; using Abp.Web.Mvc.Authorization; using AutoMapper; using LearningMpaAbp.Notifications; usi

测开之路六十:接口测试平台之common目录

实现接口测试平台使用jsonpath进行取值来断言,效果: 访问页面: 调试功能:http://www.kuaidi100.com/query 保存功能 目录结构 common的代码: init: import timeimport uuid def get_timestamp(data=None): """ 生成字符串格式的时间戳数据 20190704204826 """ if data: return time.strftime("

一、数据库表中字段的增删改查,二、路由基础.三、有名无名分组.四、多app共存的路由分配.五、多app共存时模板冲突问题.六、创建app流程.七、路由分发.八、路由别名,九、名称空间.十、反向解析.十一、2.x新特性.十二、自定义转换器

一.数据库表中字段的增删改查 ''' 直接在modules中对字段进行增删改查 然后在tools下点击Run manage.py Task执行makemigrations和migrate 注意在执行字段的删除过程中需不需要对数据进行备份 ''' 二.路由基础 ''' # url中含有四个参数 # url(regex, view, kwargs=None, name=None) # 正则路径 视图函数地址 默认关键字参数(了解) 路由别名 # r'index' 只要请求中含有index都可以匹配成

Spring Boot (十五): Spring Boot + Jpa + Thymeleaf 增删改查示例

这篇文章介绍如何使用 Jpa 和 Thymeleaf 做一个增删改查的示例. 先和大家聊聊我为什么喜欢写这种脚手架的项目,在我学习一门新技术的时候,总是想快速的搭建起一个 Demo 来试试它的效果,越简单越容易上手最好.在网上找相关资料的时候总是很麻烦,有的文章写的挺不错的但是没有源代码,有的有源代码但是文章介绍又不是很清楚,所在找资料的时候稍微有点费劲.因此在我学习 Spring Boot 的时候,会写一些最简单基本的示例项目,一方面方便其它朋友以最快的方式去了解,一方面如果我的项目需要用到相

beego增删改查代码实现

记录下使用beego的增删改查实现,数据库使用mysql,完整代码如下: package main import ( _ "crud_beego/routers" //自动注册路由 "fmt" "github.com/astaxie/beego" "github.com/astaxie/beego/orm" _ "github.com/go-sql-driver/mysql" ) const ( DRIVE