tags:

views:

658

answers:

1

Here are the objects:

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

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

Thanks

+1  A: 

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.

// 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'])));
luckytaxi