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