0

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!

2 Answers 2

3

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' => []  ]
                ]
            ]
]);
Sign up to request clarification or add additional context in comments.

1 Comment

I am getting 'pipeline' option must be specified as an array error
1

Use this way in current explanation: Pipeline must be in an array always

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.