我有以下 mongo 查询。通过这个例子,我得到一个子文档(带有查找)“transport_type”。这个子文档有很多属性。我只想要 id 和名称。如何将 $project 与 $arrayElemAt 和 $filter 结合使用?现在它返回所有字段
$stations = Station::raw(function ($collection) use ($match){
return $collection->aggregate([
[
'$lookup' => [
'as' => 'transport_type',
'from' => 'transport_types',
'foreignField' => '_id',
'localField' => 'transport_type_id'
]
],
[
'$match' => $match
],
[
'$project' => [
'name' => 1,
'code' => 1,
'area_id' => 1,
'transport_type' => [
'$arrayElemAt' => [
[
'$filter' => [
'input' => '$transport_type',
'as' => 'transport_type_',
'cond' => [ '$eq' => [ '$$transport_type_.deleted_at', null ] ],
]
], 0
]
],
'active' => 1
]
],
[
'$sort' => [
'name' => 1
]
]
]);
});