mongoose - 我如何在 Mongoose 中聚合和填充

标签 mongoose aggregate mean-stack mongoose-populate

我在填充“主要”时遇到问题 收集,分组效果很好,但我真的不知道如何 聚合后填充甚至 .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);
    });

欢迎和赞赏任何建议。

最佳答案

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

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

});

关于mongoose - 我如何在 Mongoose 中聚合和填充,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31102364/

相关文章:

node.js - 位置运算符不适用于feathersjs查询更新

node.js - 错误: Cannot find module './models/modela'

MongoDB 聚合 - 使用另一个数组过滤数组

r - 聚合或汇总以获得比率

javascript - 在 MEAN 堆栈应用程序中显示图像(服务图像)的有效方法?

javascript - Angular-UI-路由器解决不工作

javascript - 如何在 Mongoose/Express 中搜索使用多选构建的数组

r - 按组以字符格式汇总金额

用于将列值分组到列中的 PostgreSQL 查询

node.js - MEAN Stack平台有BI/OLAP工具吗?