Databases Reference
In-Depth Information
{ “_id” : { “$oid” : “4c970555be6700000000385a”} , “name” : “Srinivas Shastri” ,
“zip” : 1089.0}
{ “_id” : { “$oid” : “4c97a6555c760000000054d8”} , “name” : “Don Joe” ,
“zip” : 10001.0}
{ “_id” : { “$oid” : “4c97a7ef5c760000000054da”} , “name” : “John Doe” ,
“zip” : 94129.0}
{ “_id” : { “$oid” : “4c97add25c760000000054db”} , “name” : “Lee Chang” ,
“zip” : 94129.0 , “streetAddress” : “37000 Graham Street”}
The output of the Java program tallies with what you saw with the command-line interactive
JavaScript shell earlier in the chapter.
Now see how the same example works with PHP.
MongoDB PHP Driver
First, download the PHP driver from the MongoDB github code repository and confi gure the driver
to work with your local PHP environment. Refer to the Appendix A subsection on MongoDB
installation for further details.
A sample PHP program that connects to a local MongoDB server and lists the documents in the
location collections in the prefs database is as follows:
$connection = new Mongo( “localhost:27017” );
$collection = $connection->prefs->location;
$cursor = $collection->find();
foreach ($cursor as $id => $value) {
echo “$id: “;
var_dump( $value );
}
Available for
download on
Wrox.com
connect_to_mongodb.php
The program is succinct but does the job! Next, you see how Ruby handles this same task.
MongoDB Ruby Driver
MongoDB has drivers for all mainstream languages and Ruby is no exception. You can obtain the
driver from the MongoDB github code repository but it may be easier to simply rely on RubyGems
to manage the installation. To get ready to connect to MongoDB from Ruby, get at least the mongo
and bson gems. You can install the mongo gem as follows:
gem install mongo
The bson gem will be installed automatically. In addition, installing bson_ext may be recommended
as well.
Listing 2-3 depicts a sample Ruby program that connects to the MongoDB server and lists all the
documents in the location collection in the prefs database.
Search WWH ::




Custom Search