MongoDB-PHP:类似 JOIN 的查询 [英] MongoDB-PHP: JOIN-like query

查看:34
本文介绍了MongoDB-PHP:类似 JOIN 的查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这里是对象:

courses
{ "name" : "Biology", "_id" : ObjectId("4b0552b0f0da7d1eb6f126a1") }
students
{
        "name" : "Joe",
        "classes" : [
                {
                        "$ref" : "courses",
                        "$id" : ObjectId("4b0552b0f0da7d1eb6f126a1")
                }
        ],
        "_id" : ObjectId("4b0552e4f0da7d1eb6f126a2")
}

使用 PHP Mongo Class,我如何让所有上过生物课程的学生?

Using the PHP Mongo Class, how do I get all the students that has a biology course?

谢谢

推荐答案

您需要查询两次.我面前没有我的环境,但类似于下面的环境.我可能有第二个查询的嵌套"部分不正确.

You'll need to query twice. I dont have my environment in front of me, but something similar to what's below. I may have the "nested" portion of the second query incorrect.

//首先获取课程的ID.

// First grab the ID for the course.

$course = $collection->findOne(array("name" => "Biology"));

//接下来查询学生集合.

// Next query the students collection.

$collection->find(array("classes" => array("id" => $course['_id'])));

这篇关于MongoDB-PHP:类似 JOIN 的查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆