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