【问题标题】:How can I Aggregate and populate in mongoose我如何在猫鼬中聚合和填充
【发布时间】:2015-09-15 03:07:44
【问题描述】:

我在填充“主要”时遇到问题 收集,分组效果很好,但我真的不知道如何 聚合后填充甚至 .find。我相信我在这里左右做模特选角:

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

欢迎和赞赏任何建议。

【问题讨论】:

    标签: mongoose aggregate mean-stack mongoose-populate


    【解决方案1】:

    找到答案似乎可以解决问题。希望这会有所帮助

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

    【讨论】:

      猜你喜欢
      • 2019-08-29
      • 2018-01-30
      • 2017-01-23
      • 2015-12-23
      • 2016-12-19
      • 2016-05-09
      • 2021-08-04
      • 2016-06-11
      • 2015-03-04
      相关资源
      最近更新 更多