mongodb - MongoDB / Discord.js - 检查集合的存在
问题描述
我用 MongoDB 制作了一个不和谐的机器人,并创建了一个命令来修改我的机器人商店中的一篇文章。在修改特定文章之前,它必须已经在数据库中找到。是否可以查明在命令中键入的名称是否存在于数据库中?如果不存在,则返回错误消息。
if(!oldName) return message.channel.send('<:croix:704272420873306165> You must indicate the name of your article')
if(!newName) return message.channel.send('<:croix:704272420873306165> You must indicate the new name for your article')
if() return message.channel.send('<:croix:704272420873306165> The name of the item doesn\'t exist.')
await serverDB.set(message.guild.id, { $rename: { [ 'modules.SHOPECO.' + oldName ] : 'modules.SHOPECO.' + newName } })
await serverDB.set(message.guild.id, { $set: { [ 'modules.SHOPECO.' + `${newName}.NAME` ] : newName } })
message.channel.send("Good !")
解决方案
你可以使用Model.findOne()
它。我不再建议的原因Model.exists()
是,根据对此答案的评论,您正在尝试查询多个条件,并且可以使用简单findOne()
ArticleModel.findOne({ id: message.guild.id }, (err, res) => {
if (res.modules.SHOPECO.includes(args[0]) return message.reply('that article already exists!')
})
推荐阅读
- arrays - 如何在 PowerPoint 中使用 VBA 将用户窗体文本框中输入的变量转换为模块中的数组文本?
- tcp - F-stack Client 未连接到 F-stack Server
- pandas - 通过 List 列中的常见字符串元素映射两个数据框
- web-applications - 使用 Angular6 应用程序连接设备
- reactjs - 复制反应状态是正确的设计方法吗?
- hive - 蜂巢。是否 ALTER TABLE 表名 CHANGE name1 name2 newdatatype; 删除 name1 的下划线数据?
- ios - 如何在 Ios 中解析 Web 服务的响应?
- php - 从 HTML 表单获取值时未定义的索引
- xml - 如何使用 xquery 从输入 xml 中删除重复节点
- rest - 如何使用 Airflow DAG 调用 REST 端点