直接上代码:
const ones = await InspectTaskUser.aggregate([{
$facet: {
paginatedResult: [
{ $match: { user_id: ObjectId(_id) } },
{ $skip: (page - 1) * size },
{ $limit: size },
{
$lookup: {
from: 'inspecttasks',
localField: 'task_id',
foreignField: '_id',
as: 'task'
}
},
{ $unwind: "$task" },
],
totalCount: [
{ $match: { user_id: ObjectId(_id) } },
{ $count: 'totalCount' }
]
}
}]);
本文通过实战案例,详细解析了如何使用MongoDB的聚合管道进行复杂的数据查询与处理,包括匹配、分组、排序、连接等操作,适用于需要从大量数据中提取有价值信息的场景。
3万+

被折叠的 条评论
为什么被折叠?



