/** Returns DBObject representing object with given id */ private DBObject getById(MongoDbServer entity, String id) throws Exception { MongoClient mongoClient = new MongoClient(entity.getAttribute(SoftwareProcess.HOSTNAME)); try { DB db = mongoClient.getDB(TEST_DB); DBCollection testCollection = db.getCollection(TEST_COLLECTION); return testCollection.findOne(new BasicDBObject("_id", new ObjectId(id))); } finally { mongoClient.close(); } }
/** Inserts new object with { key: value } at given server, returns new document's id */ private String insert(MongoDbServer entity, String key, Object value) throws Exception { MongoClient mongoClient = new MongoClient(entity.getAttribute(SoftwareProcess.HOSTNAME)); try { DB db = mongoClient.getDB(TEST_DB); DBCollection testCollection = db.getCollection(TEST_COLLECTION); BasicDBObject doc = new BasicDBObject(key, value); testCollection.insert(doc); ObjectId id = (ObjectId) doc.get("_id"); return id.toString(); } finally { mongoClient.close(); } }