Skip to content
Advertisement

How to separate array from nested arrays with MongoDB and PHP aggregate?

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,

JavaScript

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.


JavaScript

What I want is as below:

JavaScript

Advertisement

Answer

What you need is a another stage after the group.

You could either use project and list each field:

JavaScript

or $replaceRoot to grab them all:

JavaScript
User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement