将回调函数传递给猫鼬聚合函数
Passing callback function to mongoose aggregate function
我正在形成要在 aggregate() 中执行的查询,如下所示:
query.$project = {};
query.$project.created_at = '$created_at';
query.$project.month = {};
query.$project.month.$month = currentMonth;
query.$match = {};
query.$match.month = currentMonth.getMonth() + 1;
query.$limit = 5;
query.$sort = {};
query.$sort.created_at = -1;
query.callback = function(err, result){
// do something
};
console.dir(query);
但是当我执行以下命令时:
DataTable.aggregate(query);
我明白了:
Error: Arguments must be aggregate pipeline operators
at Aggregate.append (C:\myproject\node_modules\mongoose\lib\aggregate.js:87:11)
at new Aggregate (C:\myproject\node_modules\mongoose\lib\aggregate.js:47:17)
at Function.aggregate (C:\myproject\node_modules\mongoose\lib\model.js:1889:17)
at C:\myproject\app\routes.js:179:23
at C:\myproject\node_modules\async\lib\async.js:570:21
at C:\myproject\node_modules\async\lib\async.js:249:17
at C:\myproject\node_modules\async\lib\async.js:125:13
我现在有两个问题:
- 有没有更好的方法来形成这个查询。
- 以及如何在此处指定回调函数。
编辑
我将上面的代码修改如下:
query.$project = {};
query.$project.created_at = '$created_at';
query.$project.month = {};
query.$project.month.$month = currentMonth;
query.$match = {};
query.$match.month = currentMonth.getMonth() + 1;
query.$limit = 5;
query.$sort = {};
query.$sort.created_at = -1;
但是当我执行以下命令时:
DataTable.aggregate(query, function(err, result){
// do something
});
引用 Mongoose's documentation for aggregate
,
Parameters:
[...] <Object, Array>
aggregation pipeline operator(s) or operator array
[callback] <Function>
期待你通过
一组聚合管道运算符
DataTable.aggregate([{
$project: {
created_at: '$created_at',
month: {
$month: currentMonth
}
}
}, {
$match: {
month: currentMonth.getMonth() + 1
}
}, {
$limit: 5
}, {
$sort: {
created_at: -1
}
}], function(err, result) {
});
或作为单独参数传递的多个聚合管道运算符
DataTable.aggregate({
$project: {
created_at: '$created_at',
month: {
$month: currentMonth
}
}
}, {
$match: {
month: currentMonth.getMonth() + 1
}
}, {
$limit: 5
}, {
$sort: {
created_at: -1
}
}, function(err, result) {
});
我更喜欢管道构建器方式,
DataTable.aggregate().project({
created_at: '$created_at',
month: {
$month: currentMonth
}
}).match({
month: currentMonth.getMonth() + 1
}).limit(5).sort({
created_at: -1
}).exec(function(err, result) {
});
编辑:如果你喜欢单独准备单独的项目,那么你可以使用数组对象,像这样
var query = [];
query.push({
$project: {
created_at: '$created_at',
month: {
$month: currentMonth
}
}
});
query.push({
$match: {
month: currentMonth.getMonth() + 1
}
});
query.push({
$limit: 5
});
query.push({
$sort: {
created_at: -1
}
});
DataTable.aggregate(query, function(err, result) {
});
或者使用管道生成器,
var aggregator = DataTable.aggregate();
...
aggregator = aggregator.project({...});
...
aggregator = aggregator.match({...});
...
aggregator = aggregator.limit(...);
...
aggregator = aggregator.sort(...);
...
aggregator.exec(function(err, result) {
});
我正在形成要在 aggregate() 中执行的查询,如下所示:
query.$project = {};
query.$project.created_at = '$created_at';
query.$project.month = {};
query.$project.month.$month = currentMonth;
query.$match = {};
query.$match.month = currentMonth.getMonth() + 1;
query.$limit = 5;
query.$sort = {};
query.$sort.created_at = -1;
query.callback = function(err, result){
// do something
};
console.dir(query);
但是当我执行以下命令时:
DataTable.aggregate(query);
我明白了:
Error: Arguments must be aggregate pipeline operators
at Aggregate.append (C:\myproject\node_modules\mongoose\lib\aggregate.js:87:11)
at new Aggregate (C:\myproject\node_modules\mongoose\lib\aggregate.js:47:17)
at Function.aggregate (C:\myproject\node_modules\mongoose\lib\model.js:1889:17)
at C:\myproject\app\routes.js:179:23
at C:\myproject\node_modules\async\lib\async.js:570:21
at C:\myproject\node_modules\async\lib\async.js:249:17
at C:\myproject\node_modules\async\lib\async.js:125:13
我现在有两个问题:
- 有没有更好的方法来形成这个查询。
- 以及如何在此处指定回调函数。
编辑
我将上面的代码修改如下:
query.$project = {};
query.$project.created_at = '$created_at';
query.$project.month = {};
query.$project.month.$month = currentMonth;
query.$match = {};
query.$match.month = currentMonth.getMonth() + 1;
query.$limit = 5;
query.$sort = {};
query.$sort.created_at = -1;
但是当我执行以下命令时:
DataTable.aggregate(query, function(err, result){
// do something
});
引用 Mongoose's documentation for aggregate
,
Parameters:
[...] <Object, Array>
aggregation pipeline operator(s) or operator array
[callback] <Function>
期待你通过
一组聚合管道运算符
DataTable.aggregate([{ $project: { created_at: '$created_at', month: { $month: currentMonth } } }, { $match: { month: currentMonth.getMonth() + 1 } }, { $limit: 5 }, { $sort: { created_at: -1 } }], function(err, result) { });
或作为单独参数传递的多个聚合管道运算符
DataTable.aggregate({ $project: { created_at: '$created_at', month: { $month: currentMonth } } }, { $match: { month: currentMonth.getMonth() + 1 } }, { $limit: 5 }, { $sort: { created_at: -1 } }, function(err, result) { });
我更喜欢管道构建器方式,
DataTable.aggregate().project({
created_at: '$created_at',
month: {
$month: currentMonth
}
}).match({
month: currentMonth.getMonth() + 1
}).limit(5).sort({
created_at: -1
}).exec(function(err, result) {
});
编辑:如果你喜欢单独准备单独的项目,那么你可以使用数组对象,像这样
var query = [];
query.push({
$project: {
created_at: '$created_at',
month: {
$month: currentMonth
}
}
});
query.push({
$match: {
month: currentMonth.getMonth() + 1
}
});
query.push({
$limit: 5
});
query.push({
$sort: {
created_at: -1
}
});
DataTable.aggregate(query, function(err, result) {
});
或者使用管道生成器,
var aggregator = DataTable.aggregate();
...
aggregator = aggregator.project({...});
...
aggregator = aggregator.match({...});
...
aggregator = aggregator.limit(...);
...
aggregator = aggregator.sort(...);
...
aggregator.exec(function(err, result) {
});