【问题标题】:How can I subtract 2 operations in mongoDB?如何在 mongoDB 中减去 2 个操作?
【发布时间】:2019-08-13 14:49:40
【问题描述】:

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

这是我目前正在处理的 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
    }
]

【问题讨论】:

    标签: node.js mongodb express


    【解决方案1】:

    请试试这个:

    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'] } } }])
    

    【讨论】:

    • 谢谢@srinivasy。你能解释一下上面的错误吗?
    • @ManfredTijerino :用您的新要求更新了我的答案,请立即查看。您的代码的问题是,您尝试在 $project 阶段对不存在的字段“management_fee”进行操作,而不是您可以在最终结果中在组和项目中执行此操作。
    • 非常感谢@srinivasy。它运作良好。我有一个问题,你为什么在你的代码中加入以下内容:平均:1,总计:1。背后的逻辑是什么?
    • 非常感谢您的支持@srinivasy。任何关于如何获得这个的想法,它肯定会帮助我。
    • 可能通过使用 JavaScript 操作 JSON 来实现,而不是通过 MongoDB 的查询来实现。
    猜你喜欢
    • 1970-01-01
    • 2018-11-22
    • 2018-06-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-05-13
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多