python - mongodb中的删除方法
问题描述
我正在尝试在 MongodB 中创建 CRUD 操作,但无法在其中创建删除操作。下面的代码用于动物收容所,我首先插入数据,然后使用 find 关键字读取数据,我只是对删除操作感到困惑,因为我想删除 - 说所有行。那么如何使用
删除许多()
from pymongo import MongoClient
class AnimalShelter(object):
""" CRUD operations for Animal collection in MongoDB """
def __init__(self, username, password):
# Initializing the MongoClient. This helps to
# access the MongoDB databases and collections.
if username and password:
self.client = MongoClient('mongodb://%s:%s@localhost:27017' % (username, password))
else:
self.client = MongoClient('mongodb://localhost:27017')
self.database = self.client['AAC']
# Complete this create method to implement the C in CRUD.
def create(self, data):
if data is not None:
insert = self.database.animals.insert(data) # data should be dictionary
if insert!=0:
return True
else:
return False
else:
raise Exception("Nothing to save, because data parameter is empty")
# Create method to implement the R in CRUD.
def read(self,criteria=None):
# criteria is not None then this find will return all rows which matches the criteria
if criteria:
# {'_id':False} just omits the unique ID of each row
data = self.database.animals.find(criteria,{"_id":False})
else:
#if there is no search criteria then all the rows will be return
data = self.database.animals.find( {} , {"_id":False})
return data
# The delete method in CRUD
(missing)
解决方案
推荐阅读
- java - Oxygen XML 编辑器和 Saxon 扩展函数
- typescript - 如何设置 TypeScript、VScode、Yarn2、Fastify 项目
- c# - 为 Blazor 网站的每个页面使用多种解决方案
- python - 如何在csv中选择一个随机单元格
- excel - 具有变量名称的 Excel 结构化引用
- swift - 只有纵向模式不适用于 iPad / Xcode / SwiftUI
- java - 是否需要索引具有唯一约束的列?
- angular - *ngFor 中的角度翻译
- python - 将列表转换为字典的最快方法,列表值作为键,列表索引作为值
- laravel - Laravel 7 whereIn 查询只返回第一个索引