首页 > 解决方案 > 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 !")

标签: mongodbmongoosediscord.js

解决方案


你可以使用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!')
})

推荐阅读