Database Reference
In-Depth Information
Similarly, you can drop an entire collection using the drop() function. This following example returns an array
with the removal results:
// Connect to the database
$c = new Mongo();
// Select the collection to remove
$collection = $c->contacts->people;
// Remove the collection and return the results
print_r($collection->drop());
The results returned look like this:
Array (
[nIndexesWas] => 1
[msg] => indexes dropped for collection
[ns] => contacts.people
[ok] => 1
)
Last but not least, you can use PHP to drop entire databases. You accomplish this by using the drop() function in
the MongoDB class, as shown in the following example:
// Connect to the database
$c = new Mongo();
// Select the database to remove
$db = $c->contacts;
// Remove the database and return the results
print_r($db->drop());
The results returned show the name of the database dropped:
Array (
[dropped] => contacts
[ok] => 1
)
DBRef
DBRef enables you to create links between two documents stored in different locations; this functionality lets you
implement behavior similar to that found in a relational database. This functionality can be particularly handy if you
want to store the addresses from the people in an addresses collection, rather than include that information in your
people collection.
There are two ways to do this. First, you can use a simple link (known as manual referencing ); in this case, you
include the _id of a document into another document. Second, you can use DBRef to create such links automatically.
First, let's look at how you implement manual referencing. In the following example, you add a contact and,
under its address information, specify the _id of another document:
// Connect to the database
$c = new MongoClient();
$db = $c->contacts;
// Select the collections we want to store our contacts and addresses in
$people = $db->people;
 
Search WWH ::




Custom Search