如何在Yii2中编写以下mongo查找查询 [英] How to write the following mongo lookup query in Yii2
本文介绍了如何在Yii2中编写以下mongo查找查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在Yii2中使用此 mongo扩展.
I am using this mongo extension in Yii2.
我有2个名为ServiceProvider
和Parents
的集合.在ServiceProvider
中,有子文档(PostCommentIDs)
,其中包含父母的ID.
I have 2 collections named ServiceProvider
and Parents
. In ServiceProvider
there is subdocument (PostCommentIDs)
containing IDs of parents.
另一个集合Parents
包含所有父信息.
Another collection Parents
contains all parent information.
我想加入2系列.我是通过以下mongo查询实现的.
I wanted to join the 2 collection. I have achieved it through the following mongo query.
但是使用上述扩展名,如何在Yii2中编写此查询.
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: [] } }
}
])
请帮助.谢谢!
推荐答案
找到了解决方案.它可能会帮助某人.
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' => [] ]
]
]
]);
这篇关于如何在Yii2中编写以下mongo查找查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文