Skip to content

Instantly share code, notes, and snippets.

View trishagee's full-sized avatar

Trisha Gee trishagee

View GitHub Profile
MongoClient mongoClient = new MongoClient(new MongoClientURI("mongodb://localhost:27017"));
MongoClient mongoClient = new MongoClient();
DB database = mongoClient.getDB("TheDatabaseName");
DBCollection collection = database.getCollection("TheCollectionName");
List<Integer> books = Arrays.asList(27464, 747854);
DBObject person = new BasicDBObject("_id", "jo")
.append("name", "Jo Bloggs")
.append("address", new BasicDBObject("street", "123 Fake St")
.append("city", "Faketon")
.append("state", "MA")
.append("zip", 12345))
.append("books", books);
MongoClient mongoClient = new MongoClient();
DB database = mongoClient.getDB("Examples");
DBCollection collection = database.getCollection("people");
collection.insert(person);
collection.insert(PersonAdaptor.toDBObject(myPerson));
DBObject query = new BasicDBObject("_id", "jo");
DBCursor cursor = collection.find(query);
DBObject jo = cursor.one();
(String)cursor.one().get("name");