1 回答
TA贡献1860条经验 获得超9个赞
您不能在find(). 你有两个选择:
您可以
$sort在您的过程中指定一个操作,$push以确保您的元素按排序顺序添加到数组中。您可以使用聚合在检索时对数组进行排序。
解决方案可能类似于以下内容(可能需要进行一些修改):
// Sorting on push.
$collection->updateOne(
[...],
[ '$push' => [
'ordens' => [
// You MUST include the $each operator here or this will not work.
'$each' => [
[ 'dtOrdem' => 5 ]
],
// Where the magic happens, sorts in descending order.
'$sort'=> [ 'dtOrdem' => -1 ]
]
]]
);
// Sorting on retrieval.
$collection->aggregate([
[ '$unwind' => '$ordens' ],
[ '$sort' => [
'$ordens.dtOrdem' => -1
]],
[ '$group' => [
'_id' => '$_id',
'ordens' => [
'$push' => '$ordens'
]
]]
]);
- 1 回答
- 0 关注
- 181 浏览
添加回答
举报
