My environment is php5.6, MongoDB 4.0.13 and mongo driver 1.6.16, and supposed I have collection/table table_x
from database db_x
as below:
code_x code_y value name color
------------------------------------------------
1 00001 111 300 aa 1
2 00001 222 100 bb 3
3 00002 111 200 cc 1
4 00002 444 400 dd 2
.....
99 00029 222 200 ee 3
100 00030 999 400 ff 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("distinct"=>"table_x", "key"=>"code_y"));
foreach ($res['values'] as $idx) {
$res[$idx]['_id'] = $idx;
}
but my expected output is below SQL query
select distinct code_y as dept from table_x