【问题标题】:How do I add a specific array to an existing document in my database?如何将特定数组添加到数据库中的现有文档?
【发布时间】:2021-08-18 05:26:28
【问题描述】:

我正在尝试为我的 Discord Bot 创建一个命令,用于将 reaction 添加到链接到 role 的特定 message 中,user 在与指定的表情符号做出反应时应该收到。

语法可能有助于更好地理解它:

!addRoleByReaction <messageID> <emoji you want to add as reaction> <role the user should receive>

示例:

!addRoleByReaction 123456789 ???? 987654321

如果用户的反应是“??????”到message 和ID '123456789' 他将收到role 和ID '987654321'


问题:

目前这是我的数据库架构:

_id: reqString,
    message: [{
        msgId: reqString,
        reactionRole: [{
            reaction: reqString,
            role: reqString,
        }],
    }],

(我正在使用 mongoDB 和 npm package 'mongoose')

添加第一个reaction 后,我的database document 看起来像这样:

但如果我添加另一个reaction,它看起来像这样:

我真正想要的……

...是推送(如果此guild 和此messageID 的条目已经存在)一个新条目(包含第二个或 第三个, ... reaction & role) 到 reactionRole 数组中。

但是现在,如您所见,它在 message 数组中添加了一个带有第二个 reaction 的新条目。总的来说,它并没有那么糟糕,但它只是丑陋..

差点忘了:这就是我向数据库添加新条目的方式:

// 'targetMessage' contains the message ID
// 'targetReaction' contains the reaction to be added
// 'role' contains the ID of the role, the user should receive
// 'mongo()' simply connects to the database

await mongo().then(async mongoose => {
    try {
        await messageReactionSchema.findOneAndUpdate({
            _id: guild.id,
            message: {
                $elemMatch: {
                    msgId: targetMessage,
                },
            },
        }, {
            _id: guild.id,
            $push: {
                message: [{
                    msgId: targetMessage,
                    reactionRole: [{
                        reaction: targetReaction,
                        role,
                    }],
                }],
            },
        }, {
            upsert: true,
        });
    }
    finally {
        mongoose.connection.close();
    }
});

【问题讨论】:

    标签: javascript node.js mongoose discord.js


    【解决方案1】:

    为了解决我的问题并避免进一步的复杂化,我现在简单地调整了schema:

    // _id reflects the id of the message
    _id: reqString,
    reactionRole: [{
        _id: false,
        reaction: reqString,
        role: reqString,
    }]
    

    如您所见,我删除了 guild.id 和 message 数组。

    现在为每条消息创建一个新的document,其中仅包含所有reactions 和关联的roles 的数组。

    由于message ID 是唯一的,并且guild 也可以被它识别,所以这种结构不会产生任何问题,并且也使得它更容易程序查找文档,因为我现在只需要搜索message ID

    【讨论】:

      【解决方案2】:

      您可以使用$ 运算符和$push 为目标消息添加角色:

      await messageReactionSchema.findOneAndUpdate({
        _id: guild.id,
        message: { msgId: targetMessage }
      }, {
        $push: {
           "message.$.reactionRole": {
              reaction: targetReaction,
              role,
           },
        },
      }...
      

      注意:

      如果您在 $elemMatch 表达式中指定单个查询谓词,并且未在 $elemMatch 中使用 $not 或 $ne 运算符,则可以省略 $elemMatch。

      【讨论】:

      • 听起来不错,我一回到家就测试这个,但已经谢谢了!
      • 那么我的message 数组中的msgId 字段呢?因为你没有在你的答案中包含这个
      • 参考你的代码,msgId字段没有变化所以你不需要把它放在更新中
      • 如果我从不将字段添加到数据库中,我应该如何根据guild.id 和msgId 查找文档?然后该字段不存在,还是我误解了什么?
      • 对不起。我忘记了这个guild 和这个messageID 的条目不存在的部分。在这种情况下,一个查询可能还不够。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-11-06
      • 1970-01-01
      • 2021-12-24
      • 2015-05-20
      • 2017-09-16
      相关资源
      最近更新 更多