@Divergent提供的解决方案确实有效,但是以我的经验,最好有两个查询:
- 首先进行过滤,然后按ID分组以获取已过滤元素的数量。不要在这里过滤,这是不必要的。
- 第二个查询,用于过滤,排序和分页。
推送$$ ROOT并使用$ slice的解决方案在大型集合中遇到了16MB的文档内存限制。同样,对于大型集合,两个查询一起运行似乎比使用$$ ROOT推送的查询运行得更快。您也可以并行运行它们,因此,仅受两个查询(可能是排序查询)中较慢的查询的限制。
我已经使用2个查询和聚合框架解决了该解决方案(请注意-在本示例中我使用node.js,但思路是相同的):
var aggregation = [
{
// If you can match fields at the begining, match as many as early as possible.
$match: {...}
},
{
// Projection.
$project: {...}
},
{
// Some things you can match only after projection or grouping, so do it now.
$match: {...}
}
];
// Copy filtering elements from the pipeline - this is the same for both counting number of fileter elements and for pagination queries.
var aggregationPaginated = aggregation.slice(0);
// Count filtered elements.
aggregation.push(
{
$group: {
_id: null,
count: { $sum: 1 }
}
}
);
// Sort in pagination query.
aggregationPaginated.push(
{
$sort: sorting
}
);
// Paginate.
aggregationPaginated.push(
{
$limit: skip + length
},
{
$skip: skip
}
);
// I use mongoose.
// Get total count.
model.count(function(errCount, totalCount) {
// Count filtered.
model.aggregate(aggregation)
.allowDiskUse(true)
.exec(
function(errFind, documents) {
if (errFind) {
// Errors.
res.status(503);
return res.json({
'success': false,
'response': 'err_counting'
});
}
else {
// Number of filtered elements.
var numFiltered = documents[0].count;
// Filter, sort and pagiante.
model.request.aggregate(aggregationPaginated)
.allowDiskUse(true)
.exec(
function(errFindP, documentsP) {
if (errFindP) {
// Errors.
res.status(503);
return res.json({
'success': false,
'response': 'err_pagination'
});
}
else {
return res.json({
'success': true,
'recordsTotal': totalCount,
'recordsFiltered': numFiltered,
'response': documentsP
});
}
});
}
});
});