问题
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