好的,我想将查询从MySQL转换成MongoDB,因为我需要它来优化我的数据,但是到目前为止我什么都没做...
MySQL查询
SELECT notifications.entity_id
FROM notifications
INNER JOIN discussions
ON notifications.entity_id = discussions._id
WHERE notifications.subscribers = 1
无论我尝试什么,我似乎都无法接近JOIN另一个表...我已经在PHP中以简单的方式完成了该操作,但是由于优化不足或没有优化,这会引起很多麻烦... < / p>
public function getData($userId) {
$wheres = ['subscribers' => $userId];
$data = $this->get($wheres, ['entity_id']); # works for notifications table that I have predefined for this function
$wheres = ['_id' => $data['_id']];
$data = $this->get_discussions($wheres, []); #queries discussions table
return $data;
}
答案 0 :(得分:1)
我对此mysql查询的解决方案是
db.notifications.aggregate({
{$match : {subscribers : 1}},
$lookup:{
from:"discussions",
localField:"entity_id",
foreignField:"id",
as:"entityids"
},
{ "$unwind": "$entityids" }
})
在mongo shell上运行此命令。您也可以使用php方法在PHP代码中触发此操作。希望对您有帮助。