【发布时间】:2018-10-10 22:16:45
【问题描述】:
所以我遇到了需要使用 mongoose 从 NodeJS 中的两个集合中获取结果的问题。基本上我有users 收藏和boughtItems 收藏。
users 集合由_id 和username 组成
boughtItems 集合由item_id、username(购买项目的用户)组成
有人可以展示如何从users 和boughtItems 获取结果,所以当我显示所有users 时,他们购买的商品将显示在同一页面中他们的名字旁边。
我有两个模型:User 和 BoughtItem
User.aggregate([{
$lookup: {
from: "boughtitems", // collection name in db
localField: "username",
foreignField: "username",
as: "boughtitems"
}
}]).exec(function(err, users) {
console.log(users);
});
此代码块用购买的物品返回用户,但每个购买的物品都是 [Object] 如何从 object 获取完整信息?
所以我做了JSON.stringify(users),现在我得到了所有信息。
我明白了:
如何在渲染文件中显示购买的物品,如下所示:
{{#each users}}
{{this.boughtitem['item_power']}}
{{/each}}
【问题讨论】: