0

How to get other related column values from MongoDB aggregate group value in PHP?

I can get distinct value by below code in PHP5.6. The database db_x with table/collection table_x, expecting $group columns, there are also some other column is what I need, can I get other columns value?

$res = $db_x->command (
        array(
            "aggregate" => "table_x",
            "pipeline" =>
                array(
                    array( '$group' => array( "_id" => ['id' =>'$code_y', 'name' => '$code_x', 'color' => '$color']))
                array( "$replaceRoot" => array( "newRoot" => "$_id" ))
                ),
            "cursor" => ['batchSize' => 200]
        )
);
Elsa
  • 1
  • 1
  • 8
  • 27
  • @Chilarai I would like to get `group by` results, not `distinct` only has one field value – Elsa Aug 11 '20 at 04:42
  • Which MongoDB PHP package did you use? And also what is `$test->command`? Provide more details in your question – Chilarai Aug 11 '20 at 06:33
  • php5.6 and mongodb 4.0.13 – Elsa Aug 11 '20 at 06:34
  • I meant which PHP-mongodb connection manager ? See this https://stackoverflow.com/questions/45705786/mongoclient-class-vs-mongodb-driver-manager-class – Chilarai Aug 11 '20 at 06:37
  • 1.6.16, that doesn't matter, I guess – Elsa Aug 11 '20 at 06:39
  • After the `$group` stage the only fields that exist will be the ones mentioned in the group. In order to get other fields, include them in the `$group` stage using one of the accumulator operators. – Joe Aug 13 '20 at 01:33
  • But I don't need to accumulator any field...... – Elsa Aug 13 '20 at 01:58
  • Only accumulator operations can be used in `$group`. If you just want to pick one of the values, try `$first` or `$last` – Joe Aug 13 '20 at 02:05
  • Got it, thanks so much – Elsa Aug 13 '20 at 02:07
  • @Joe Hi, nobody answered, I only if ask you for help! I am sorry to bother you again and again, do you know to get all related aggregate values by field name with MongoDB and PHP? php https://stackoverflow.com/questions/63381352/get-all-related-aggregate-values-by-field-name-with-mongodb-and-php – Elsa Aug 13 '20 at 03:29

0 Answers0