MongoDB Group /匹配总和

时间:2016-02-09 16:22:15

标签: mongodb

我有以下聚合器:

db.user.aggregate([{ $match : { type : "follow" } },
                        {$group:{_id: { $month: "$createdDate" }, 
                        follow:{$sum:1}} } ]);

并输出:

{
    "_id" : 9,
    "follow" : 36
}

/* 2 */
{
    "_id" : 8,
    "follow" : 12
}

但是我需要添加另一个语句,所以输出看起来像

{
    "_id" : 9,
    "follow" : 36,
    "unfollow" : 1
}

/* 2 */
{
    "_id" : 8,
    "follow" : 12,
    "unfollow" : 3
}

如何实现这一目标?

1 个答案:

答案 0 :(得分:1)

利用$cond管道步骤中的$group运算符,根据<?php $html = ""; $variable = "Hello World"; $html .= <<<HTML <!-- Your html content --> {$variable} HTML;//Need to be always on the left $variable2 = "Hi"; $html .= <<<HTML <h2>{$variable2}</h2> HTML; echo $html; ?> 值评估计数,如下所示:

type