Database Reference
In-Depth Information
PHP arrays can also be nested. This complex document contains an array of tags and a
sub-document with a last_access date and integer pingtime . Note you must use the
special MongoDate class to represent a date:
$doc = array( "url" => "org.mongodb",
"tags" => array( "database", "open-source"),
"attrs" => array( "last_access" => new MongoDate(),
"pingtime" => 20
)
);
D.1.2
Connections
You can connect to a single node with the Mongo constructor:
$conn = new Mongo( "localhost", 27017 );
To c o n n e c t t o a r e p li c a s e t , p a s s a M o n g o D B c o n n e c t i o n URI to the Mongo constructor.
You must also specify array( "replicaSet" => true ) :
$repl_conn = new Mongo( "mongo://localhost:30000,localhost:30001",
array( "replicaSet" => true ));
MONGODB CONNECTION URIS The MongoDB connection URI is a standard way
to specify connection options across drivers. Most of the drivers will accept a
connection URI , and this can simplify configuration for a system that talks to a
MongoDB server across environments. See the official online MongoDB docs
for the latest URI specification.
PHP applications often run much more efficiently with persistent connections. If you
use them, be sure always to specify array( "persistent" => "x" ) , where "x" repre-
sents a unique identifier for the persistent connection being created:
$conn = new Mongo( "localhost", 27017, array( "persist" => "x" ) );
D.1.3
Sample program
The following PHP program shows how to insert, update, query, and delete a docu-
ment. It also includes several PHP BSON document representations.
Listing D.1
Sample PHP driver usage
<?php
$m = new Mongo( "localhost", 27017 );
$db = $m->crawler;
$coll = $db->sites;
$doc = array( "url" => "org.mongodb",
"tags" => array( "database", "open-source"),
"attrs" => array( "last_access" => new MongoDate(),
"pingtime" => 20
)
);
$coll->insert( $doc );
 
Search WWH ::




Custom Search