donglian1384 2016-06-15 09:59
浏览 192
已采纳

PHP MongoDB聚合:只有当值大于0时,如何$ sum?

I am using PHP to access a MongoDB collection in which I have recorded players of a game :

{username: "John", stats: {games_played: 79, boosters_used: 1, crystals: 5}},
{username: "Bill", stats: {games_played: 0, boosters_used: 0, crystals: 20}},
{username: "Jane", stats: {games_played: 154, boosters_used: 14, crystals: 37}},
{username: "Sarah", stats: {games_played: 22, boosters_used: 0, crystals: 0}},
{username: "Thomas", stats: {games_played: 0, boosters_used: 0, crystals: 20}},

In my PHP script I am doing this to get sums and averages :

$filter = [
    ['$group' => [
        'Players count' => ['$sum' => 1],
        'avgGamesPlayed' => [
            '$avg' => '$stats.games_played'
        ],
        'TotalGamesPlayed' => [
            '$sum' => '$stats.games_played'
        ],
    ]],
    ['$sort' => [get('sort', 'count') => (int) get('sort_order', -1)]],
];

$options = [];

$m->aggregate($filter, $options);

If I echo the result I'll obtain :

Players count = 5;
avgGamesPlayed = 51;
TotalGamesPlayed = 255;

What I would like to do is to get the $sum of players where stats.games_played is greater than 0. In this particular case the result would be 3.

I know that there is a possibility to do this if I use find and '$gt' => 0 but I really need to stick with aggregate. Si I am trying to do something like this :

'Players count' => ['$sum' => ['$gt' => 0]],

But it doesn't work and I'm stuck here for weeks now, I read the doc but I'm not that familiar with MongoDB, that's why I'm calling for your knowledges.

If you have the answer to this question I'd appreciate it a lot, thank you.

  • 写回答

1条回答 默认 最新

  • dongsu0308 2016-06-15 10:07
    关注

    This is where the $cond operator fits nicely. You can use it as an expression within the $sum operator's document that will evaluate the logic and returns 0 when the $stats.games_played field evaluates to 0 and 1 otherwise (that is > 0).

    Following this approach will yield the desire outcome:

    $pipeline = [
        ['$group' => [
            'Players count' => [
                '$sum' => [
                    '$cond' => [ [ '$gt' => [ '$stats.games_played',  0 ] ], 1, 0 ] 
                ]
            ],
            'avgGamesPlayed' => [
                '$avg' => '$stats.games_played'
            ],
            'TotalGamesPlayed' => [
                '$sum' => '$stats.games_played'
            ],
        ]],
        ['$sort' => [get('sort', 'count') => (int) get('sort_order', -1)]],
    ];
    
    $options = [];
    
    $m->aggregate($pipeline, $options);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?