0

我正在尝试按字段 publish_target 对计数进行分组

使用 MYSQL 我刚刚做了:

$postsByPlatform = $db->query("SELECT COUNT(*) as `value`, `publish_target` as `key` FROM `Post` GROUP BY `publish_target` ");
$postsByPlatform = $postsByPlatform->fetchAll(PDO::FETCH_OBJ);
$return['Post']['publish_target'] = $postsByPlatform;

并得到这样的结果:

publish_target: [
{
value: "22",
key: "facebook"
},
{
value: "33",
key: "googleplus"
},
{
value: "44",
key: "instagram"
},
{
value: "55",
key: "other"
},
{
value: "66",
key: "twitter"
}
],

我们现在正在转换为 Mongo。

我已经在 PHP.net 上尝试了这个例子,适应了这个:

        // use all fields
        $keys = array('publish_target');

// set intial values
        $initial = array("count" => 0);

// JavaScript function to perform
        $reduce = "function (obj, prev) { prev.count++; }";

// only use documents where the "a" field is greater than 1

        $g = $collection->group($keys, $initial, $reduce);

        var_dump($g);

但这只是返回

array(4) {
  'retval' =>
  array(1) {
    [0] =>
    array(1) {
      'count' =>
      double(100)
    }
  }
  'count' =>
  double(100)
  'keys' =>
  int(1)
  'ok' =>
  double(1)
}

这对我来说毫无意义。

我该如何正确地做到这一点?

4

1 回答 1

1

我不确定这是否是问题,但与我自己的代码比较这一行

$keys = array('publish_target');

应该是这样的

$keys = array('publish_target' => true);
于 2013-02-05T17:31:06.830 回答