node.js - 如何在 mongoDB 中减去 2 个操作?

标签 node.js mongodb express

我想计算一个名为management_fee的对象的lower_limit,为了计算这个变量,我需要获得management_fee的平均值,然后我需要获得标准差。计算完两个变量后,我可以减去平均值减去标准差。

这是我当前正在开发的 JSO 示例:

{
        "_id": "5d44ef5ea366bfcdc21",
        "user": "5d2f594faa4182c47c",
        "management_fee": 399,
        "cancellation_date": "2017-05-02T00:00:00.000Z",
        "country": "USA",
        "state": "Texas",
        "__v": 0
    },
    {
    "_id": "5d44ef5ea366bfcdc22",
        "user": "5d2f594faa4182c42c",
        "management_fee": 599,
        "cancellation_date": "2017-08-09T00:00:00.000Z",
        "country": "USA",
        "state": "Texas",
        "__v": 0
    },
   {
    "_id": "5d44ef5ea366bfcdc23",
        "user": "5d2f594faa4182c48c",
        "management_fee": 700,
        "cancellation_date": "2017-08-09T00:00:00.000Z",
        "country": "USA",
        "state": "New York",
        "__v": 0
    }

在下面的代码中,我想按年份对 lower_limit 进行分组,然后通过计算 management_fee 的平均值和偏差标准来计算,这样我就可以将两者相减。

    router.get('/random_test_10', auth, async (req, res) => {

      try {

        const model_CancellationKPI = await CancellationKPI.aggregate([
          {
            $group: {
              _id: {year:{$year: "$cancellation_date"}},
              lower_limit: {$subtract:[ {std:{$stdDevSamp: "$management_fee"}, avr:{$avg:"$management_fee"}}]}, 

            }
          }
        ])

       res.json(model_CancellationKPI);

      } catch (err) {
        console.error(err.message); 
        res.status(500).send('Server Error')
      }

    });

下面提供的答案效果很好。但是,现在我想在同一输出中添加总计和平均值,但我收到 null 和 0。你知道为什么吗?

这是我的代码:

router.get('/random_test_11', auth, async (req, res) => {

      try {

        const model_CancellationKPI = await CancellationKPI.aggregate([{
          $group: {
              _id: { year: { $year: "$cancellation_date" } }, 
              std: { $stdDevSamp: '$management_fee' }, avg: { $avg: '$management_fee' }
          }}, 

          { $project: { year: '$_id.year', _id: 0, 
          result: { $subtract: ['$avg', '$std'] }, average: {$avg: '$management_fee'}, 
          total: {$sum: '$management_fee}'}
          } }])


       res.json(model_CancellationKPI);

      } catch (err) {
        console.error(err.message); 
        res.status(500).send('Server Error')
      }

    });

这是输出:

[
    {
        "year": 2017,
        "result": 388.90493206697874,
        "average": null,
        "total": 0
    },
    {
        "year": 2019,
        "result": 88.03232361610253,
        "average": null,
        "total": 0
    },
    {
        "year": 2018,
        "result": 367.4573075192308,
        "average": null,
        "total": 0
    }
]

最佳答案

请尝试这个:

db.getCollection('CancellationKPI').aggregate([{
    $group: {
        _id: { year: { $year: "$cancellation_date" } }, std: { $stdDevSamp: '$management_fee' },total :{$sum :'$management_fee'}, average: { $avg: '$management_fee' }
    }
}, { $project: { year: '$_id.year', _id: 0, average: 1, total: 1, result: { $subtract: ['$average', '$std'] } } }])

关于node.js - 如何在 mongoDB 中减去 2 个操作?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57468247/

相关文章:

mysql - 添加事务以 Sequelize 创建模块

Node.js 中的 JavaScript 原型(prototype)对象效率

javascript - ExpressJS 应用程序使用

node.js - 在没有 sudo 的情况下安装 Node.js/npm 的最佳方法

mongodb - Ansible-Playbook 无法安装 MongoDB

mongodb - 无法运行 mongodb 因为核心转储问题?

javascript - gulp-watch 无休止地编译而不对文件进行更改

Python 数据库调节问题

node.js - 测试快速路线方法

node.js - 使用 Express 和 node-archiver 即时生成 zip 存档