Group groups and project operators

I am trying to group "group_name" and select the fields "group_name" and "group_id" using the php driver using the aggregation structure:

Array ( [$project] => Array ( [group_name] => 1 [group_id] => 1 ) [$group] => Array ( [_id] => $group_name [total_sum] => Array ( [$sum] => 1 ) ) ) 

I get this: [errmsg] => exception: A pipeline stage specification object must contain exactly one field. However, when I use only the $project or $group operator, it works fine.

+1
source share
2 answers

The problem is that you are grouping your $ project and $ group together.

Your array should look more like

 { $project: { group_name: 1, group_id: 1 }, }, { $group:{ _id:{ group_name:'$group_name', }, total_sum:{$sum:1} } } 
+4
source

You will need to reformat, and also use commas as separators. This is a pipeline, so it takes each element at a time.

I have not tried this, but I will try:

 array ( array( array( '$project' => array( "group_name" => 1, "group_id" => 1, ) ), array( '$group' => array( "_id" => array("group_name" => '$group_name'), "total_sum" => array('$sum' => 1), ), ) ) 
0
source

All Articles