2015-02-08 82 views
2

我形成了查詢內聚集()如下執行:傳遞迴調函數,以貓鼬聚合函數

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 
}); 
+2

你構建會是什麼樣子查詢對象:'{$ project:{},$ match:{},$ sort:{}}',這是不正確的。而構建的可能看起來像一個構建正確的查詢:'[{$項目:{}},{$比賽:{}}]'如下面的答案從@thefourtheye提及。 – BatScream 2015-02-08 06:22:47

回答

5

引用Mongoose's documentation for aggregate

參數:

  1. [...] <Object, Array>聚集管道運營商(S)或運營商陣列

  2. [callback] <Function>

它期待你傳遞

  1. 聚合管道運營商

    DataTable.aggregate([{ 
        $project: { 
         created_at: '$created_at', 
         month: { 
          $month: currentMonth 
         } 
        } 
    }, { 
        $match: { 
         month: currentMonth.getMonth() + 1 
        } 
    }, { 
        $limit: 5 
    }, { 
        $sort: { 
         created_at: -1 
        } 
    }], function(err, result) { 
    
    }); 
    
  2. 或多個聚合管道運營商作爲單獨的參數

    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) { 

}); 
+0

能將它我形成查詢的方式來完成。我有這個語法的問題。 – jsbisht 2015-02-08 05:15:12

+0

@jsbisht存儲我們傳遞在第一方法中的變量的數組。這對你有用嗎? – thefourtheye 2015-02-08 05:16:01

+0

我想這會工作。你可以添加到您的答案 – jsbisht 2015-02-08 05:17:47