【发布时间】:2018-06-05 02:16:23
【问题描述】:
我有Label、Conversations 和Messages 模型...
标签包含对话 id...我使用了 $lookup,它适用于对话
const conversation = await Label.aggregate([
{ $match: { _id: mongoose.Types.ObjectId("5abcb74bb59afe4310c60266") } },
{ $lookup: {
from: 'conversations',
localField: 'conversations',
foreignField: '_id',
as: "conversations"
}
}])
并给出类似
的输出[{
_id: 5abcb74bb59afe4310c60266,
name: 'Archive',
value: 'archive',
user: 5abc93a85d3914318cc8d3d7,
conversations: [ [Object], [Object] ],
__v: 0
}]
现在我在conversations 里面有messages
[{
_id: 5abe07717a978c41b270b1bc,
messages: [ 5abe07717a978c41b270b1bd ],
members: [ 5abc93a85d3914318cc8d3d7, 5abc9467b7c99332f0a6813c ],
__v: 0
}]
那么我如何为里面的消息字段应用 $lookup
标签 -> 对话 -> 消息
提前致谢
【问题讨论】:
标签: node.js mongodb mongoose mongodb-query aggregation-framework