RT,例如下面这种业务场景:
const docs = await SendMessage.aggregate([
{
$match: {
channel_id: Types.ObjectId(id),
}
},
{
$project: {
text: '$msg_content',
createdAt: '$send_time',
user: {
_id: '$msg_from', // 某个userId
name: null, // 如何根据userId在这里给它相关username
},
}
},
{
$sort: {
_id: -1,
}
},
])
我在用
mongoose
,后来发现aggregate
下面$lookup
这个方法可以解决问题!