【问题标题】:Moongose: update record pushing element into arrayMongoose:更新记录将元素推入数组
【发布时间】:2022-01-22 22:32:36
【问题描述】:

对不起,我的英语很差,希望你明白我的意思。

我有一个包含许多表的 mongoDb 数据库。在我的“文件”表中,所有记录都有一个名为“etiquetas”的字段。该字段是一个字符串数组。我想更新此字段(在 ID XXXX 的记录中)插入新字符串。我已经阅读了 mongoose 的文档,并尝试使用“push”函数更新记录,但 mongoose 将对象而不是字符串插入到数组“etiquetas”中。

让我告诉你发生了什么。这就是我更新记录所做的事情(在 nodejs 应用程序中):

let exampleString = "61c43b346c6168d039f8c0gg"; // Using an example string
await File.updateOne(
  { id: fileId },
  { $push: { etiquetas: { exampleString } } }
);

这就是正在发生的事情(我手动插入了数组“etiquetas”的第一条记录):

click to see what it happens

【问题讨论】:

    标签: javascript node.js mongodb mongoose


    【解决方案1】:

    尝试删除exampleString周围的花括号:

    let exampleString = "61c43b346c6168d039f8c0gg"; // Using an example string
    await File.updateOne(
      { id: fileId },
      { $push: { etiquetas: exampleString } }
    );
    

    【讨论】:

    • 非常感谢。我觉得我很愚蠢......哈哈
    猜你喜欢
    • 2020-12-12
    • 2016-01-20
    • 2020-04-18
    • 1970-01-01
    • 2021-03-09
    • 2018-07-12
    • 2015-12-09
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多