具有cond,sort和limit的mongodb组

时间:2018-10-08 02:43:55

标签: mongodb mongodb-query aggregation-framework

我正在尝试基于利润汇总字段,但是我使用不同的货币字段。我不知道如何创建一个可以评估多个表达式的$ cond表达式。

db.getCollection("ps_days").aggregate([
    {"$group" : {_id:"$userName", handsCount:{$sum: "$hands"}, profit: {$sum: "$profit"}}},
    {$sort:{"profit":-1}},
    {$limit: 50}
])

此查询显示组利润。但是我需要检查货币列中的货币,如果它是欧元,则将其乘以1.1,如果是英镑,则将其乘以1.4。货币列中的值:$,€,£。 我的最后一个查询:

db.getCollection("888_days").aggregate(
[
    {
        $match: {
           currency: '€'
        }
    },
    {
        $group: {
            '_id': "$userName", 
            'handsCount': {$sum: "$hands"}, 
            'profit': {
              $sum: {
                $cond: [
                    {$eq: ['$currency', "\$"]},
                    '$profit',
                    {$cond: [
                          {$eq: ['$currency', '€']},
                          {$multiply: ['$profit', 1.1]},
                          {$multiply: ['$profit', 1.4]}
                    ]}
                ]
              }
            }
        }
    },
    {
        $sort: {
            'profit': -1
        }
    },
    {
        $limit: 50
    },
]);

但是它不起作用。可以在请求中执行我的任务吗?

0 个答案:

没有答案