Php 从mysql到mongodb_的逆向工程是最合适的方法

Php 从mysql到mongodb_的逆向工程是最合适的方法,php,mysql,mongodb,Php,Mysql,Mongodb,在mysql中,我们写道 SELECT * FROM Student , Course WHERE Student.course_id = Course.id AND Course.name = "English" 我们可以在mongodb中做与(在PHP中)相同的事情 或者,还有另一种方法使用预联接或嵌入集合,如下所示 $m = new MongoClient(); $db = $m->selectDB('test'); $collection1 = new MongoCollec

在mysql中,我们写道

SELECT * 
FROM Student , Course
WHERE Student.course_id = Course.id 
AND Course.name = "English"
我们可以在mongodb中做与(在PHP中)相同的事情

或者,还有另一种方法使用预联接或嵌入集合,如下所示

$m = new MongoClient();
$db = $m->selectDB('test');
$collection1 = new MongoCollection($db, 'Student');
$cursor = $collection1->find(array("name"=>"Course.English"));

//do something with result
嵌入如下

Student=>array(

id=>...,

Course=>[]

)
1) .我不知道什么是最好的解决办法。
2) 。在一个项目中,如果两个集合也被多次单独使用,最好的方法是什么。

由于经常使用两个集合,嵌入两个集合会导致操作变慢。(分别访问集合中的数据)因此在这里可以使用以下代码

$m = new MongoClient();
$db = $m->selectDB('test');
$collection1 = new MongoCollection($db, 'Course');
$cursor = $collection1->find(array("name"=>"English"));

$collection2 = new MongoCollection($db, 'Student');


foreach($cursor as $res) {

    $cursor = $collection2->find(array("course_id"=>array($in=>$res['id'])));
    //do something with the result
}

in here if you need to retrive data from collection2,

$resArr = array();
foreach($cursor as $res) {

array_push($resArr ,$res['id']);

}

  $cursor2 = $collection2->find(array("course_id"=>array($in=>$resArr)));


foreach($cursor2 as $res2) {

    //do something with the result
}

我不明白你想做什么。。。看起来像是加入?是的,但mongodb中没有所谓的加入概念。所以我们必须这样做。是的,没有连接。然而,现在还不清楚你想做什么。通常使用
$in
操作符从二级集合中获取相关文档的批。是的,这是我的错误。我编辑过。
$m = new MongoClient();
$db = $m->selectDB('test');
$collection1 = new MongoCollection($db, 'Course');
$cursor = $collection1->find(array("name"=>"English"));

$collection2 = new MongoCollection($db, 'Student');


foreach($cursor as $res) {

    $cursor = $collection2->find(array("course_id"=>array($in=>$res['id'])));
    //do something with the result
}

in here if you need to retrive data from collection2,

$resArr = array();
foreach($cursor as $res) {

array_push($resArr ,$res['id']);

}

  $cursor2 = $collection2->find(array("course_id"=>array($in=>$resArr)));


foreach($cursor2 as $res2) {

    //do something with the result
}