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

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);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 R语言卸载之后无法重装,显示电脑存在下载某些较大二进制文件行为,怎么办
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?
  • ¥15 关于#vue.js#的问题:修改用户信息功能图片无法回显,数据库中只存了一张图片(相关搜索:字符串)
  • ¥15 texstudio的问题,