PHP中mongoDB的内部连接
好吧,我正试图将这个查询从MySQL翻译成MongoDB,因为我需要它来优化我的数据,但到目前为止我还没有取得任何成果 MySQL查询PHP中mongoDB的内部连接,php,mysql,mongodb,Php,Mysql,Mongodb,好吧,我正试图将这个查询从MySQL翻译成MongoDB,因为我需要它来优化我的数据,但到目前为止我还没有取得任何成果 MySQL查询 SELECT notifications.entity_id FROM notifications INNER JOIN discussions ON notifications.entity_id = discussions._id WHERE notifications.subscribers = 1 不管我怎么做,我似乎都无法接近另一张桌子。。。我用
SELECT notifications.entity_id
FROM notifications
INNER JOIN discussions
ON notifications.entity_id = discussions._id
WHERE notifications.subscribers = 1
不管我怎么做,我似乎都无法接近另一张桌子。。。我用PHP简单地完成了这项工作,但由于优化程度很低,甚至没有优化,这项工作带来了很多麻烦
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;
}
我的mysql查询解决方案是
db.notifications.aggregate({
{$match : {subscribers : 1}},
$lookup:{
from:"discussions",
localField:"entity_id",
foreignField:"id",
as:"entityids"
},
{ "$unwind": "$entityids" }
})
在mongo shell上运行此命令。您也可以使用PHP方法在PHP代码中启动此功能。希望这将对您有所帮助。或的可能副本