javascript - 将新的卡片对象推送到嵌套的对象数组中

标签 javascript node.js mongodb mongoose

我的Accounts模型中有这个结构:

{
  cards: {
    starter: [],
    intermediate: [],
    advanced: [ {Object}, {Object}, {Object} ]
  },
  firstName: 'Sepideh',
  email: '<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="6a190f1a030e0f022a0f120b071a060f44090507" rel="noreferrer noopener nofollow">[email protected]</a>',
}

上面 cards.advanced 数组中的 Objects 如下所示:

{
  cards: [
    { // this is a single card object 
      cardTitle: 'this card is for you',
      performance: [],
      cardID: 32,
    }
  ],
  unit: 3 // this is the unit we want to push our new card object into
}

假设我可以使用给定的电子邮件访问上述文档(或换句话说帐户):

const account = await db.Account.findOne({ email: '<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="2e5d4b5e474a4b466e4b564f435e424b004d4143" rel="noreferrer noopener nofollow">[email protected]</a>' });

我们如何将一个新的卡片对象推送到带有 unit: 3 (是的,到目前为止这是唯一的单位)属性的嵌套卡片数组中,并像这样保存 Sepideh 帐户:

{
    cards: {
        starter: [],
        intermediate: [],
        advanced: [
            {
                cards: [
                    { // this is a single card object 
                        cardTitle: 'this card is for you',
                        performance: [],
                        cardID: 32,
                    }

                    { // this is new pushed card object 
                        cardTitle: 'this card is for me',
                        performance: [],
                        cardID: 33,
                    }
                ],
                unit: 3 // this is the unit we want to push our new card object into
            },
            {Object},
            {Object}
        ] // end of advanced array
    },
    firstName: 'Sepideh',
    email: '<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="ed9e889d84898885ad88958c809d8188c38e8280" rel="noreferrer noopener nofollow">[email protected]</a>',
}

我尝试使用这些人来选择文档的第 3 单元,但没有一个起作用:

const userUnit = await account.findOne({'cards.advanced.unit': unit});

const userUnit = await account.findOne({'cards.advanced.unit': unit});

const userUnit = await account.find({}, {'cards.advanced.unit': {$elemMatch: {unit: unit}}}); 

const userUnit = await db.Account.findOne({ email: email, 'cards.advanced.unit': unit});

然后我会将新卡对象推送到 userUnit 并调用 await account.save();

唯一有效的是纯 JavaScript 代码,如下所示:

const userUnit = account.cards.advanced.find(e => e.unit == unit);

这次我无法保存对数据库的更改...(我不知道如何保存它..)

你会怎么做?

最佳答案

您可以使用$push运算符直接与 $ positional operator 一起使用它允许您指定应更新哪个子元素:

await db.Account.update({ email: "<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="f083958099949598b09588919d809c95de939f9d" rel="noreferrer noopener nofollow">[email protected]</a>", "cards.advanced.unit": 3 },
        { $push: { "cards.advanced.$.cards": { cardTitle: 'this card is for me', performance: [], cardID: 33 } } })

如果您希望动态评估您的路径,您可以使用 computed property names :

let path = "advanced";
await db.Account.update(
    { email: "<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="85f6e0f5ece1e0edc5e0fde4e8f5e9e0abe6eae8" rel="noreferrer noopener nofollow">[email protected]</a>", ["cards." + path + ".unit"]: 3 },            
    { $push: { ["cards." + path + ".$.cards"]: { cardTitle: 'this card is for me', performance: [], cardID: 33 } } })

Mongo Playground

关于javascript - 将新的卡片对象推送到嵌套的对象数组中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68410218/

相关文章:

javascript - Wordpress 中的谷歌图表

javascript - 如何将新的/更新的innerHTML 附加到没有类或id 的p 标记?

javascript - html div onclick 事件

javascript - JS Puppeteer 等待页面加载完成

python - Javascript 可以加载 Python pickle 转储的字符串但不能加载文件

node.js - 使用 Mongoose 前置数组的正确方法?

node.js - 如何使用 Sails.js v1 中的 .find() 方法进行不区分大小写的搜索

JavaScript - 在不影响索引的情况下将元素插入数组?

javascript - Meteor - 将 mongodb 查询分配给变量并将其插入到另一个集合

nginx - Node.js 和 nginx SSL 握手失败