【发布时间】:2015-03-24 05:12:35
【问题描述】:
我正在使用 mongoose/nodejs 应用程序构建分页并具有以下代码:
var Query = Model.find(
"location": {
"$geoWithin": {
"$box": [
[165.8694369, -52.61941849999999],
[175.831536, -29.2313419]
]
}
}
});
// Get count of documents
return Query.count(function (err, totalCount) {
if (err) throw err;
// Get paginated list
return Query.
find().
skip(skip).
limit(limit).
exec(function (err, results) {
if (err) throw err;
// return result
});
在前面的代码中,我得到totalCount 在客户端构建分页,它工作正常。但现在我需要使用 aggregate 而不是 find 并尝试以下操作:
// Construct pipeline
var pipeline = [{
"$geoNear": {
"near": [165.8694369, -52.61941849999999],
"distanceField": "distance"
}
}];
var Query = adModel.aggregate(pipeline);
// Get count of documents
return Query.count(function (err, totalCount) {
if (err) throw err;
但不幸的是,我收到了错误 count() method does not exists。如何使用聚合框架计算文档总数?
【问题讨论】:
标签: node.js mongodb mongoose aggregation-framework