【问题标题】:MongoDB keeps giving me nullMongoDB一直给我null
【发布时间】:2013-05-17 01:49:22
【问题描述】:

我在让 Mongodb 聚合工作时遇到了一些困难。它一直给我null。请帮忙。下面是用php编写的代码。谢谢。

我想要做的是总结 2 个特定日期之间 2 个字段的值,请求和响应

    try {
        $mongodb = new MongoClient("mongodb://ad:pass2word1@localhost");

        $database = $mongodb->selectDB('backend');
        $collection = new MongoCollection($database, 'RequestSummary');

        $pipeline = array(
            array(
                '$group' => array(
                    '_id' => array(
                        'request' => array('$sum' => '$Requests'),
                        'response' => array('$sum' => '$Responses')
                    )
                )
            ),
            array(
                '$match' => array(
                    'RequestDate' => array(
                        '$gte' => intval($_SESSION['range_from']),
                        '$lte' => intval($_SESSION['range_to'])
                    )
                )
            )
        );
        $collection->aggregate($pipeline);
        var_dump($g);
    } catch (MongoConnectionException $exc) {
        echo $exc->getTraceAsString();
    }

【问题讨论】:

    标签: mongodb mongodb-php


    【解决方案1】:

    $group 的 _id 不能包含像 $sum 这样的聚合运算符。这些总和需要定义为与_id 相同级别的字段。如果您不想在特定字段上进行分组,您可以使用 NULL 作为 _id,如下所示:

     array(
        '$group' => array(
            '_id' => NULL,
            'request' => array('$sum' => '$Requests'),
            'response' => array('$sum' => '$Responses')
        )
    ),
    

    【讨论】:

      猜你喜欢
      • 2016-07-11
      • 1970-01-01
      • 1970-01-01
      • 2021-11-20
      • 2022-11-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-10-23
      相关资源
      最近更新 更多