drws65968272 2016-06-13 08:58
浏览 58

将工作MongoDB聚合管道查询转换为PHP的麻烦

I'm using MongoChef to construct an aggregation pipeline command that performs a $match, then a $group then a $project.

The following code produces the correct output and is confirmed working in MongoDB itself:

db.collection_name.aggregate(
  [
    {
      $match: { 
          ":energy_mon_id" : 9
      }
    },
    {
      $group: { 
          "_id" : {
              "Date" : "$:date"
          }, 
          "avg_voltage_a" : {
              "$avg" : "$:voltage_a"
          }, 
          "avg_voltage_b" : {
              "$avg" : "$:voltage_b"
          }, 
          "avg_voltage_c" : {
              "$avg" : "$:voltage_c"
          }
      }
    },
    {
      $project: { 
          "avg_volt" : {
              "$add" : [
                  "$avg_voltage_a", 
                  "$avg_voltage_b"
              ]
          }
      }
    },
    {
      $match: { 
      }
    }
  ]
);

The output that is produced is this (note that the avg_volt values are integers):

{ "_id" : { "Date" : "2016-06-06" }, "avg_volt" : 779 }
{ "_id" : { "Date" : "2016-06-08" }, "avg_volt" : 779 }

Now the issue I'm faced with is getting this to run correctly in PHP.

My code in PHP is below:

$collection = $this->db->testdb->collection_name;

    $aggrCommand = array(
      array(
        '$match' => array( ":energy_mon_id" =>  9)
      ),
    array( 
      '$group' => array( 
        "_id" => array( 
          "Date" => "$:date"
          ), 
        "avg_voltage_a" => array( 
          '$avg' => "$:voltage_a"
          ),
        "avg_voltage_b" => array( 
          '$avg' => "$:voltage_b"
          ),
        "avg_voltage_c" => array( 
          '$avg' => "$:voltage_c"
          ),
        )
    ),
     array(
      '$project' => array(
        "avg_volt" => array(
          '$add' => ["$avg_voltage_a","$avg_voltage_b" ]
          )
        )
      )
    );

    $list = $collection->aggregate( $aggrCommand); 

The error I get is for this very last line when I try to submit a GET request via postman:

Slim Application Error
The application could not run because of the following error:
Details
Type: MongoDB\Driver\Exception\RuntimeException
Code: 16554
Message: $add only supports numeric or date types, not String
File: C:\wamp\www\DRM\vendor\mongodb\mongodb\src\Operation\Aggregate.php
Line: 168

This makes no sense at all since the output in MongoDB is an integer value, and I can't spot any errors in my conversion to PHP.

One thing to note is that my field names do actually contain a colon at the front (its not a mistake), but I don't think that is the issue here.

I'm really stumped with this, can someone provide any advice on how to figure this issue out? The output from my MongoDB command is clearly not a string, yet in PHP it says the values of avg_voltage_a and avg_voltage_b are strings. This makes no sense at all.

  • 写回答

1条回答 默认 最新

  • doushu0591 2016-06-13 12:47
    关注

    Try with single quotes in your "$add" statement to prevent PHP interpreting "$avg_voltage_a" as a variable :

    '$add' => ['$avg_voltage_a','$avg_voltage_b' ]
    
    评论

报告相同问题?

悬赏问题

  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料