Database Reference
In-Depth Information
print "Initial document:n";
print print_r( $doc );
print "Updating pingtime...n";
$coll->update(
array( "_id" => $doc["_id"] ),
array( '$set' => array( 'attrs.pingtime' => 30 ) )
);
print "After update:n";
$cursor = $coll->find();
print print_r( $cursor->getNext() );
print "nNumber of site documents: " . $coll->count() . "n";
print "Removing documents...n";
$coll->remove();
?>
D.2
Java
Among MongoDB drivers, the Java driver may be the one most frequently used in pro-
duction. In addition to backing pure Java apps, the Java driver also forms the basis for
the drivers powering JVM languages like Scala, Clojure, and JRuby. Java's lack of a dic-
tionary literal makes the building of BSON documents more verbose, but the driver on
the whole is still easy to use.
D.2.1
Documents
To c o n s t r u c t a BSON document, you can initialize a BasicBSONObject , which imple-
ments the Map interface to provide a simple API centered around get() and put()
operations.
The BasicBSONObject constructor takes an optional initial key-value pair for con-
venience. Using that, you can build a simple document like so:
DBObject simple = new BasicDBObject( "username", "Jones" );
simple.put( "zip", 10011 );
Adding a sub-document means creating an extra BasicBSONObject . The array can be
a normal Java array:
DBObject doc = new BasicDBObject();
String[] tags = { "database", "open-source" };
doc.put("url", "org.mongodb");
doc.put("tags", tags);
DBObject attrs = new BasicDBObject();
attrs.put( "lastAccess", new Date() );
attrs.put( "pingtime", 20 );
doc.put( "attrs", attrs );
System.out.println( doc.toString() );
Finally, note that you can inspect a document using its toString() method.
Search WWH ::




Custom Search