简体   繁体   English

无法使聚合函数起作用

[英]Can't get aggregate functions to work

I'm completely new to Mongo, and I'm having some serious trouble wrapping my head around the Aggregation Framework. 我对Mongo完全不熟悉,而且我在围绕聚合框架时遇到了一些麻烦。 I have a document that looks like this: 我有一个看起来像这样的文档:

record {
    _id: '2013-04-22/tacos',
    first: {
        something: {0: 1, 1:4},
        somethingelse: {0: 18, 1:22}
    },
    second: {
        0: [11, 18, 7],
        1: [17, 9, 22]
    },
    metadata: {
        date: '2013-04-22'
    }
}

What I want to do is get sums (since this will need to work with multiple documents) of each item in first.something, each item in second, and then sums of all items in first.something , second.<whatever>.0 , second.<whatever>.1 , and second.<whatever>.2 . 我想要做的是在first.something中获取每个项目的总和(因为这将需要使用多个文档),每秒中的每个项目,然后是first.somethingsecond.<whatever>.0的所有项目的first.something second.<whatever>.0second.<whatever>.1second.<whatever>.2 So I'm looking for a result like this (pardon my formatting): 所以我正在寻找这样的结果(原谅我的格式化):

sum(first.something.0):      1
sum(first.something.1):      4
sum(second.0.0):            11
sum(second.0.1):            18
sum(second.0.2):             7
sum(second.1.0):            17
sum(second.1.1):             9
sum(second.1.2):            22
sum(first.something.all):    5
sum(second.<whatever>.0):   28
sum(second.<whatever>.1):   27
sum(second.<whatever>.2):   29

Right now I'm just trying to get the first two working, on a single record, and I've got this: 现在我只是试着让前两个工作,在一个记录上,我得到了这个:

// $db is my collection, and is verified to work
$aggregate = array(
    '$group' => array(
        '_id' => '$metadata.date',
        'first_zero' => array('$sum' => 'first.something.0'),
        'first_one' => array('$sum' => 'first.something.1')
    ),
    '$match' => array(
        "metadata.date" => '2013-04-22'
    )
);
$cursor = $db->aggregate($aggregate);
var_dump($cursor);

However my output tells me exception: A pipeline stage specification object must contain exactly one field. 但是我的输出告诉我exception: A pipeline stage specification object must contain exactly one field. . But if I leave out the '$match' part, it returns the record with int(0) for first_zero and first_one as shown: 但是如果我省略'$match'部分,它会返回第一个first_zero和第first_one int(0)记录,如下所示:

array(2) {
  ["result"]=>
  array(1) {
    [0]=>
    array(3) {
      ["_id"]=>
      string(10) "2013-04-22"
      ["clicks_zero"]=>
      int(0)
      ["clicks_one"]=>
      int(0)
    }
  }
  ["ok"]=>
  float(1)
}

So I can't even get the simple part working. 所以我甚至无法让简单的部分工作。 What am I doing wrong here? 我在这做错了什么? And is the second.<whatever>.0 even a thing I can do? 并且是second.<whatever>.0即便是我可以做的事情?

I'm no expert in Php but should the aggregation query look like: 我不是Php的专家,但聚合查询应该是这样的:

  $aggregate = array(
      array('$group' => array(
          '_id' => '$metadata.date',
          'first_zero' => array('$sum' => 'first.something.0'),
          'first_one' => array('$sum' => 'first.something.1')
      )),
      array('$match' => array(
          "metadata.date" => '2013-04-22'
      ))
  );

Each pipeline operator should be inside a hash/array. 每个管道运算符都应该在哈希/数组中。

Edit 编辑

However, the above would give you no result because of the $match pipeline at the end. 但是,由于最后的$match管道,上面的内容不会给你带来任何结果。 There is no metadata.date field that is created in the previous pipeline, so no results will match your query. 在上一个管道中没有创建metadata.date字段,因此没有结果与您的查询匹配。 The right solution would be: 正确的解决方案是:

  $aggregate = array(
      array('$match' => array(
          "metadata.date" => '2013-04-22'
      )),
      array('$group' => array(
          '_id' => '$metadata.date',
          'first_zero' => array('$sum' => 'first.something.0'),
          'first_one' => array('$sum' => 'first.something.1')
      ))
  );

Which will give you: 哪个会给你:

{
"result" : [
    {
        "_id" : "2013-04-22",
        "first_zero" : 1,
        "first_one" : 4
    }
],
"ok" : 1
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM