【发布时间】:2022-01-25 13:14:04
【问题描述】:
我正在尝试根据chatroomId 获取消息并返回该聊天室中的所有消息,并从userId 添加有关用户的所有数据。我尝试了以下方法,但它返回一个空数组。
我做错了什么?
chatMessageSchema.statics.getConversationByRoomId = async function (
chatRoomId,
options = {}
) {
try {
return this.aggregate([
{ $match: { chatRoomId } },
{ $sort: { createdAt: -1 } },
{
$lookup: {
from: "User",
localField: "userId",
foreignField: "_id",
as: "user",
},
},
{ $unwind: "$user" },
{ $skip: options.page * options.limit },
{ $limit: options.limit },
{ $sort: { createdAt: 1 } },
]);
} catch (error) {
throw error;
}
};
【问题讨论】:
标签: node.js database mongodb rest express