Database Reference
In-Depth Information
conn.setWriteConcern( w );
DB db = conn.getDB( "crawler" );
DBCollection coll = db.getCollection( "sites" );
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 );
coll.insert(doc);
System.out.println( "Initial document:n" );
System.out.println( doc.toString() );
System.out.println( "Updating pingtime...n" );
coll.update( new BasicDBObject( "_id", doc.get("_id") ),
new BasicDBObject( "$set", new BasicDBObject( "pingtime", 30 ) ) );
DBCursor cursor = coll.find();
System.out.println( "After updaten" );
System.out.println( cursor.next().toString() );
System.out.println( "Number of site documents: " + coll.count() );
System.out.println( "Removing documents...n" );
coll.remove( new BasicDBObject() );
}
}
D.3
C++
Reasons to recommend the C++ driver include its speed and its closeness to the core
server. You'd be hard pressed to find a faster driver, and if you're interested in
MongoDB's internals, then learning the C++ driver makes for a good entry point into
the source code. The C++ driver isn't so much a standalone driver as an integral inter-
nal MongoDB API that's intermingled with the core code base. That said, facilities
exist for using this code as an independent library.
D.3.1
Documents
There are two ways to create BSON documents in C++. You can use the somewhat ver-
bose BSONObjBuilder , or you can use the BSON macros that wrap it. I'll show both
methods for each example document.
Let's start with a simple document:
BSONObjBuilder simple;
simple.genOID().append("username", "Jones").append( "zip", 10011 );
BSONObj doc = simple.obj();
cout << doc.jsonString();
Search WWH ::




Custom Search