Mongoose 聚合 $LOOKUP 和 $match

问题描述 投票:0回答:1

我要从 reviewRatings 获取数据并使用 $lookup 来获取 userdetails.data,但在这里我需要在 $lookup 中获取数据匹配,其中 userdetails.data.name='abc' 或 userdetails.data.email="[email受保护]

$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;
mongodb mongodb-query aggregate-functions lookup
1个回答
0
投票

尝试将管道添加到您的

$lookup
阶段。

根据你的语法认为你正在使用php,我想你可以尝试这样的事情:

['$lookup' => [
                    'from' => 'userdetails',
                    'localField' => 'user_id',
                    'foreignField' => '_id',
                    'as' => 'combined',
                    'pipeline' => [
                        ['$match' => 
                            ['$expr' => 
                                ['$or' => [
                                    ['$eq' => ['$data.name', 'abc']], 
                                    ['$eq' => ['$data.email', '[email protected]']]
                                ]]
                            ]
                        ]
                     ]
               ]],
© www.soinside.com 2019 - 2024. All rights reserved.