The MongoDBRef class
Giriş
This class can be used to create lightweight links between objects in different collections.
Motivation: Suppose we need to refer to point to a document in another collection. The easiest way is to create a field in the current document, something like:
<?php
$people = $db->selectCollection("people");
$addresses = $db->selectCollection("addresses");
$myAddress = array("line 1" => "123 Main Street",
"line 2" => null,
"city" => "Springfield",
"state" => "Vermont",
"country" => "USA");
// save the address
$addresses->insert($myAddress);
// save a person with a reference to the address
$me = array("name" => "Fred", "address" => $myAddress['_id']);
$people->insert($me);
?>
Suppose now that we have a more general case, where we don't know what collection contains the referenced document. MongoDBRef is a good choice for this case, as it is a common format that all of the drivers and the database understand.
<?php
$people = $db->selectCollection("people");
$trainRef = MongoDBRef::create("hobbies", $modelTrains['_id']);
$soccerRef = MongoDBRef::create("sports", $soccer['_id']);
// now we'll know what collections the items in the "likes" array came from when
// we retrieve this document
$people->insert(array("name" => "Fred", "likes" => array($trainRef, $soccerRef)));
?>
İçindekiler
- MongoDBRef::create — Creates a new database reference
- MongoDBRef::get — Fetches the object pointed to by a reference
- MongoDBRef::isRef — Checks if an array is a database reference
MongoDBRef
There are no user contributed notes for this page.
