2015-06-28 2 views
3

У меня возникли проблемы с заполнением коллекции «Main» , группировка работает очень хорошо, но я действительно не знаю, как заполнить или даже .find после агрегирования. Я считаю, что я делаю отливку модели здесь или около того:Как я могу заполнить и заполнить в mongoose

Main.aggregate([ 
     {$match : query}, 
     { 
     $group:{ 
      _id: queryGroupBy, 
      activated: {$sum: '$activated'}, 
      componentTitle: {$first:'$componentTitle'}, 
      titlePrefix: {$first:'$titlePrefix'}, 
      operator_name: {$first:'$operator_name'} 
     } 
     }, 
     { 
     $project:{ 
      _id: '$_id', 
      summation: '$activated', 
      componentTitle: '$componentTitle', 
      titlePrefix: '$titlePrefix', 
      operator_name: '$operator_name' 
     } 
     }], 
     function(err,results) { 
     if (err) throw err; 
     result = results.map(function(doc) { 
      doc._id = doc._id, 
      doc.activated = doc.activated, 
      doc.componentTitle = doc.componentTitle, 
      doc.titlePrefix = doc.titlePrefix, 
      doc.operator_name = doc.operator_name, 
      doc.fssStatusFDD = "", 
      doc.dateUpdated = "", 
      delete doc._id; 
      delete doc.summation; 

      var _main = new Main(); 
      _main = doc; 
      console.log('test3'); 
      return _main 
      }); 
     Main.populate(results, { "path": "operator_name" }, function(err,results) { 
      if (err) throw err; 
      console.log(JSON.stringify(results, undefined, 4)); 
     }); 

     console.log('good'); 
     return res.send(results); 
    }); 

Любое предложение приветствуется и оценивается.

ответ

10

Нашел ответ, кажется, что это сделает трюк. надеюсь, что это поможет

Main.aggregate([ 
    {$match : query}, 
    { 
    $group:{ 
     _id: queryGroupBy, 
     activated: {$sum: '$activated'}, 
     componentTitle: {$first:'$componentTitle'}, 
     titlePrefix: {$first:'$titlePrefix'}, 
     operator_name: {$first:'$operator_name'} 
    } 
    }, 
    { 
    $project:{ 
     _id: '$_id', 
     summation: '$activated', 
     componentTitle: '$componentTitle', 
     titlePrefix: '$titlePrefix', 
     operator_name: '$operator_name' 
    } 
    }], 
    function(err,results) { 
    Main.populate(results, { "path": "operator_name" }, function(err,results) { 
     if (err) throw err; 
     console.log(JSON.stringify(results, undefined, 4)); 
     console.log('good'); 
     return res.send(results); 
    }); 

}); 
Смежные вопросы