How to write the following mongo lookup query in Yii2

ぐ巨炮叔叔 提交于 2019-12-10 12:14:25

问题


I am using this mongo extension in Yii2.

I have 2 collections named ServiceProvider and Parents. In ServiceProvider there is subdocument (PostCommentIDs) containing IDs of parents.

Another collection Parents contains all parent information.

I wanted to join the 2 collection. I have achieved it through the following mongo query.

But using the above extension how do I write this query in Yii2.

db.ServiceProvider.aggregate([
   {
      $unwind: "$PostCommentIDs"
   },
   {
      $lookup:
         {
            from: "Parents",
            localField: "PostCommentIDs",
            foreignField: "ID",
            as: "ParentDetails"
        }
   },
   {
      $match: { "ParentDetails": { $ne: [] } }
   }
])

Please help. Thanks!


回答1:


Found the solution. It may help someone.

$collection = Yii::$app->mongodb->getCollection('ServiceProvider');
$result = $collection->aggregate(
            ['$unwind' => '$PostCommentUserIDs'],
            [ 
                '$lookup' => 
                    [
                        'from' => 'Parents',
                        'localField' => 'PostCommentUserIDs',
                        'foreignField' => 'ID',
                        'as' => 'ParentDetails'
                    ] 
            ],
            [
                '$match' => [
                    'ParentDetails' => [ '$ne' => []  ]
                ]
            ]
);


来源:https://stackoverflow.com/questions/36951391/how-to-write-the-following-mongo-lookup-query-in-yii2

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!