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

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. 关于国内注册codepen。无法收到邮件问题的解决

    我刚刚使用的qq邮箱也无法收到.后来使用了@foxmail.com邮箱就可以了. 我记得以前注册国外的一些东西,使用qq邮箱也是无法收到. 你可以在qq邮箱里面注册一个英文邮箱.注册以后就是@foxm ...

  2. php编程怎么和mysql连接

    php连接mysql的方法: MySQLi - 面向对象 MySQLi - 面向过程 关闭连接 连接在脚本执行完后会自动关闭.你也可以使用以下代码来关闭连接: (MySQLi - 面向对象 MySQL ...

  3. Collection -集合祖宗的常用七种共性方法

    package cn.learn.collection; import java.util.ArrayList; import java.util.Collection; /* 在java.util. ...

  4. python小学堂1

    sun=0 start=1 while True: start1=start%2 if start1==1: sun = start + sun elif start1==0: sun=sun-sta ...

  5. Android关于Activity生命周期详解

    子曰:溫故而知新,可以為師矣.<論語> 学习技术也一样,对于技术文档或者经典的技术书籍来说,指望看一遍就完全掌握,那基本不大可能,所以我们需要经常回过头再仔细研读几遍,以领悟到作者的思想精 ...

  6. CHEVP算法(Canny/Hough Estimation of Vanishing Points)

    这个算法是汪悦在 Lane detection and tracking using B-spline中提出来的.他在这篇论文中主要用的是B-spline模型,这个模型的主要优点是鲁棒性好,可以针对不 ...

  7. Linux文件行排序

    sort:对文件的行排序 - 准备一份文件:char.txt - sort char.txt:结果会按照头字母顺序排 - sort -o sortchar.txt char.txt:排序char.tx ...

  8. CSS世界中的“盒子”

    1.块级元素 HTML标签通常被分为两类:块级元素和内联元素. “块级元素”和“display为block的元素”不是同一个概念.例如<li>元素默认的display值为list-item ...

  9. CSS-05 html和body标签

    html和body标签 一直对这两个标签有迷惑,查了一些网上资料整理了一下. 1.html和body标签的背景 1.当给body一个背景色时候,背景图是充满整个窗口的,这里看上去是body标签下的背景 ...

  10. 【彩彩只能变身队(第七组)】Beta版本

    本篇博客包括前期博文汇总.任务墙.团队管理细节与交流细节.代码管理.Beta阶段冲刺.团队总结.用户使用报告.Postmortem报告. 服务器网址:http://47.106.227.154/ 彩彩 ...