As $
positional operator doesn't work 2 level deep nested array, I am using alternative schema to enable update function for nested array.
I have a nested documents as follows
{
'_id' : 1234,
'bio' : {
'achievements' : {
'Yhg87Hghg65' : {
'title' : 'Achievement 1',
'score' : 95,
'year' : 2004
},
'67gjfygt8Hd' : {
'title' : 'Achievement 2',
'score' : 89,
'year' : 2003
},
'Lkoh8hHggf7' : {
'title' : 'Achievement 1',
'score' : 90,
'year' : 2005
}
}
}
}
Now, using mongodb aggregation pipeline, I can fetch this as follow in PHP
$doc = $collection -> aggregate(
array(
'$match' => array(
'_id' => 1234
)
),
array(
'$project' => array(
'bio.achievements' => 1
)
)
);
Until now, everything works fine. But I need to sort achievements by year. Using normal '$sort'
before '$project'
wont work because because achievements aren't arrays, they are fields whose values are array. If anyone know how to achieve it, please give me some hints.