gpt4 book ai didi

javascript - 如何在嵌套文档中填充对模型的多个引用?

转载 作者:太空宇宙 更新时间:2023-11-04 02:27:33 25 4
gpt4 key购买 nike

我有一个需要填充的大嵌套文档。

var PortfolioSchema = new Schema({
bonds : {
percentage : Number,
USA : {
percentage : Number,
treasury : {
percentage : Number,
securities : [ {
percentage : Number,
info : { type: ObjectId, ref: 'security' }
} ]
},
municipal : {
percentage : Number,
securities : [ {
percentage : Number,
info : { type: ObjectId, ref: 'security' }
} ]
},
corporate : {
percentage : Number,
securities : [ {
percentage : Number,
info : { type: ObjectId, ref: 'security' }
} ]
}
},
international : {
percentage : Number,
developed : {
percentage : Number,
securities : [ {
percentage : Number,
info : { type: ObjectId, ref: 'security' }
} ]
},
emergent : {
percentage : Number,
securities : [ {
percentage : Number,
info : { type: ObjectId, ref: 'security' }
} ]
}
}
},
stocks : {
percentage : Number,
USA : {
percentage : Number,
largeCaps : {
percentage : Number,
securities : [ {
percentage : Number,
info : { type: ObjectId, ref: 'security' }
} ]
},
mediumCaps : {
percentage : Number,
securities : [ {
percentage : Number,
info : { type: ObjectId, ref: 'security' }
} ]
},
smallCaps : {
percentage : Number,
securities : [ {
percentage : Number,
info : { type: ObjectId, ref: 'security' }
} ]
}
},
international : {
percentage : Number,
developed : {
percentage : Number,
securities : [ {
percentage : Number,
info : { type: ObjectId, ref: 'security' }
} ]
},
emergent : {
percentage : Number,
securities : [ {
percentage : Number,
info : { type: ObjectId, ref: 'security' }
} ]
}
}
},
_goal : { type: ObjectId, ref: 'goal' },
createdAt : { type: Date, default: Date.now },
updatedAt : { type: Date, default: Date.now }
});

如何在所有这些字段中填充security?我已经尝试过下面的代码,但它不起作用。

Portfolio
.findById(req.params.portfolio_id)
.populate("security")
.exec(function (err, portfolio) {
if (err) {
return res.status(404).json("Portfolio not found.");
}
res.render('portfolios/show.ejs', {
portfolio: portfolio
});
});

最佳答案

我刚刚想通了...我所要做的就是填充每一个引用的字段:

Portfolio
.findById(req.params.portfolio_id)
.populate("bonds.USA.treasury.securities.info")
.populate("bonds.USA.municipal.securities.info")
.populate("bonds.USA.corporate.securities.info")
.populate("bonds.international.developed.securities.info")
.populate("bonds.international.emergent.securities.info")
.populate("stocks.USA.largeCaps.securities.info")
.populate("stocks.USA.mediumCaps.securities.info")
.populate("stocks.USA.smallCaps.securities.info")
.populate("stocks.international.developed.securities.info")
.populate("stocks.international.emergent.securities.info")
.exec(function (err, portfolio) {
if (err) {
return res.status(404).json("Portfolio not found.");
}
res.render('portfolios/show.ejs', {
portfolio: portfolio
});
});

关于javascript - 如何在嵌套文档中填充对模型的多个引用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29371959/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com