2016-04-30 102 views
0

我在Yii2中使用这个mongo extension如何在Yii2中编写下面的mongo查询查询

我有2个系列,分别命名为ServiceProviderParents。在ServiceProvider中有子文档(PostCommentIDs)包含父母的ID。

另一个集合Parents包含所有父级信息。

我想加入2集合。我通过下面的mongo查询完成了它。

但是使用上面的扩展名我该如何在Yii2中编写这个查询。

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

请帮忙。谢谢!

回答

0

找到了解决方案。它可以帮助某人。

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