Skip to content

Instantly share code, notes, and snippets.

View trishagee's full-sized avatar

Trisha Gee trishagee

View GitHub Profile
collection.update(new BasicDBObject("_id", "jo"),
new BasicDBObject("$set", new BasicDBObject("phone", "5559874321")));
DBObject jo = // get the document representing jo
jo.put("name", "Jo In Disguise"); // replace the old name with the new one
collection.update(new BasicDBObject("_id", "jo"), // find jo by ID
jo); // set the document in the DB to the new document for Jo
collection.createIndex(new BasicDBObject("fieldToIndex", 1));
DBObject findLondoners = new BasicDBObject("address.city", "London");
collection.find(findLondoners));
DBCursor results = collection.find(new BasicDBObject("name", new BasicDBObject("$gt", 10)));
DBCursor results = collection.find(new BasicDBObject("numberOfOrders", new BasicDBObject("$gt", 10)));
DBCursor results = collection.find(new BasicDBObject("name", "SomeName"),
new BasicDBObject("address", 0));
DBCursor results = collection.find(new BasicDBObject("name", "SomeName"),
new BasicDBObject("name", 1));
for (DBObject result : results) {
// do something with each result
}
results.size();