Mongo projection with $arrayElemAt

775 views Asked by At

I have the following mongo query. With this example I get a subdocument (with lookup) "transport_type". This subdocument have many properties. I want only the id and the name. How can I combine $project with $arrayElemAt and $filter? Right now It returns all the fields

$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
                ]
            ]
        ]);
    });
1

There are 1 answers

0
Michalis On
$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' => [
                        '$let' => [
                            'vars' => [
                                'field' => [
                                    '$arrayElemAt' => [
                                        [
                                            '$filter' => [
                                                'input' => '$transport_type',
                                                'as' => 'transport_type_',
                                                'cond' => [ '$eq' => [ '$$transport_type_.deleted_at', null ] ]
                                            ]

                                        ], 0
                                    ]
                                ]
                            ],
                            'in' => [
                                ['id' => '$$field._id','name' => '$$field.name']
                            ]
                        ]
                    ],
                    'active' => 1

                ]
            ],
            [
                '$sort' => [
                    'name' => 1
                ]
            ]
        ]);
    });