我有这个代码:
app.get('/notifications/:id', function(req, res) {
Notification.find({
userId: req.params.id
}, '_id type initiatorId', function(err, notifications) {
if (err) return;
// grab all users by the `initiatorId`
});
});
notifications
看起来像这样:
[
{
initiatorId: 1
},
{
initiatorId: 2
},
{
initiatorId: 3
}
]
但是,我需要从/users
每个initiatorId
s 的集合中获取用户详细信息。产生这种结构的最佳方法是什么:
[
{
initiatorId: 1,
user: {
name: 'john'
}
},
{
initiatorId: 2,
user: {
name: 'larry'
}
},
{
initiatorId: 3,
user: {
name: 'moe'
}
}
]