我形成了查詢內聚集()如下執行:傳遞迴調函數,以貓鼬聚合函數
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
});
你構建會是什麼樣子查詢對象:'{$ project:{},$ match:{},$ sort:{}}',這是不正確的。而構建的可能看起來像一個構建正確的查詢:'[{$項目:{}},{$比賽:{}}]'如下面的答案從@thefourtheye提及。 – BatScream 2015-02-08 06:22:47