0

我有以下 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
                ]
            ]
        ]);
    });
4

1 回答 1

1
$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
                ]
            ]
        ]);
    });
于 2017-08-29T22:10:42.787 回答