-1

I would like to get the distinct code_y values, and I use below code, the output is equal to select distinct code_y from table_x,

$res = $db_x->command (
        array(
            "aggregate" => "table_x",
            "pipeline" =>
                array(
                    array( '$group' => array( "_id" => ['id' =>'$code_y', 'name' => '$code_x', 'color' => '$color']))),
            "cursor" => ['batchSize' => 200]
        )
);

I got results as below, I don't need [_id] => Array, just id, name and color, How to separate array from nested arrays with MongoDB and PHP aggregate? Or how to put [_id] => Array together with id, name and color? as in my front-end htmls pages, I got [object] [Object] because of those nested arrays.


   [0] => Array
        (
            [_id] => Array
                (
                    [id] => a1
                    [name] => bbb
                    [color] => blue
                )

        )

    [1] => Array
        (
            [_id] => Array
                (
                    [id] => a2
                    [name] => aaa
                    [color] => blue
                )

        )

    [2] => Array
        (
            [_id] => Array
                (
                    [id] => a3
                    [name] => abc
                    [color] =>red
                )

        )

What I want is as below:

[0] => Array
    (
       [id] => a1
       [name] => bbb
       [color] => blue
    )

[1] => Array
    (
       [id] => a2
       [name] => aaa
       [color] => blue


    )

[2] => Array
    (
      [id] => a3
      [name] => abc
      [color] =>red
    )
Elsa
  • 1
  • 1
  • 8
  • 27
  • Can you add to your aggregate `[$unwind => "$_id"]` i.e `array(''$unwind" => "$_id")` – Gibbs Aug 12 '20 at 07:21
  • you mean change the third line to `"aggregate" => "table_x",['$unwind' => "$_id"],`? it doesn't work,it returns nothing – Elsa Aug 12 '20 at 07:24
  • No, asking you to add another one after group – Gibbs Aug 12 '20 at 07:25
  • It doesn't work, I add and test it several places, all got nothing. – Elsa Aug 12 '20 at 07:31
  • Could you please update the query which you tried to the question? – Gibbs Aug 12 '20 at 07:36
  • $unwind doesn't work, no need to update, I guess, you can try it locallay – Elsa Aug 12 '20 at 07:38
  • What do you want the output to look like? – Joe Aug 12 '20 at 08:29
  • What I want is as below: ``` [0] => Array ( [id] => a1 [name] => bbb [color] => blue ) [1] => Array ( [id] => a2 [name] => aaa [color] => blue ) [2] => Array ( [id] => a3 [name] => abc [color] =>red ) – Elsa Aug 12 '20 at 08:37
  • @Joe hey, I already updated my quesiton, thanks for reading my posts. – Elsa Aug 12 '20 at 08:38

1 Answers1

1

What you need is a another stage after the group.

You could either use project and list each field:

array( '$project' => array( 
     "id" => '$_id.id',
     "name" => '$_id.name',
     "color" => '$_id.color'
))

or $replaceRoot to grab them all:

array( '$replaceRoot' => array( "newRoot" => '$_id' ))
Elsa
  • 1
  • 1
  • 8
  • 27
Joe
  • 25,000
  • 3
  • 22
  • 44
  • Dear Joe, the second `$replaceRoot` solved my problem!!! You saved my day!!!!!!Thanks so so so much!!!!!!!!!! – Elsa Aug 12 '20 at 08:56
  • Hi Joe, do you know how to get selecting fields from MongoDB aggregate group value in PHP? If you know it, I invite you to answer my other question https://stackoverflow.com/questions/63351032/how-to-get-selecting-fields-from-mongodb-aggregate-group-value-in-php – Elsa Aug 12 '20 at 16:00