Mongoose aggregate $LOOKUP with $match

35 Views Asked by At

I am to fetching data from reviewRatings and used $lookup to get userdetails.data, but here i need to get data matching in $lookup where userdetails.data.name='abc' or userdetails.data.email="[email protected]"

$results = $this->mongo_db->aggregate(
                'reviewRatings',
                [
                    ['$match' => $where],
                    ['$sort' => ['_id' => -1]],
                    ['$skip' => (int)$start],
                    ['$limit' => $limit],
                    ['$lookup' => [
                        'from' => 'userdetails',
                        'localField' => 'user_id',
                        'foreignField' => '_id',
                        'as' => 'combined'
                    ]],
                    ['$project' => [
                        '_id' => 1,
                        'app_id' => 1,
                        'user_id' => 1,
                        'rating' => 1,
                        'received_on' => 1,
                        'review_date' => 1,
                        'max_rating' => 1,
                        'location' => 1,
                        'connection_type' => 1,
                        'is_replied' => 1,
                        'recommendation_type' => 1,
                        'combined._id' => 1,
                        'combined.data' => 1
                    ]]

                ]
            );
 return $results;
1

There are 1 best solutions below

0
Fourchette On BEST ANSWER

Try adding a pipeline to your $lookup stage.

According to your syntax think you are using php, i think you can try something like this :

['$lookup' => [
                    'from' => 'userdetails',
                    'localField' => 'user_id',
                    'foreignField' => '_id',
                    'as' => 'combined',
                    'pipeline' => [
                        ['$match' => 
                            ['$expr' => 
                                ['$or' => [
                                    ['$eq' => ['$data.name', 'abc']], 
                                    ['$eq' => ['$data.email', '[email protected]']]
                                ]]
                            ]
                        ]
                     ]
               ]],