List of usage examples for com.mongodb MongoClient getDB
@Deprecated public DB getDB(final String dbName)
From source file:com.crosstreelabs.cognitio.service.mongo.MongoReferenceService.java
License:Apache License
public MongoReferenceService(final MongoClient client) { this.client = client; db = client.getDB("cognitio"); if (!db.collectionExists("references")) { references = db.createCollection("references", null); // references.createIndex(new BasicDBObject("location", 1), new BasicDBObject("unique", true)); } else {/*from ww w .j a v a2 s. co m*/ references = db.getCollection("references"); } }
From source file:com.damanzano.mongohw2_2.MongoHW2_2.java
public static void main(String[] args) throws UnknownHostException { MongoClient client = new MongoClient(); DB db = client.getDB("students"); DBCollection collection = db.getCollection("grades"); DBCursor allGrades = collection.find(new BasicDBObject("type", "homework")) .sort(new BasicDBObject("student_id", 1).append("score", 1)); try {//from w ww . java2 s . c om String studentId = ""; while (allGrades.hasNext()) { DBObject grade = allGrades.next(); System.out.println(grade); if (!studentId.equalsIgnoreCase(((Integer) grade.get("student_id")).toString())) { System.out.println("Deleting grade " + grade.get("_id").toString() + " for student " + grade.get("student_id").toString()); collection.remove(grade); } studentId = ((Integer) grade.get("student_id")).toString(); } } finally { allGrades.close(); } }
From source file:com.daprota.m2.manager.MongoManager.java
License:Apache License
public MongoManager(DbConnection dbConn) throws M2Exception { if (dbConn != null) { try {// ww w.jav a 2 s . c o m MongoClient mongoClient = dbConn.getMongoClient(); if (mongoClient != null) { String dbName = dbConn.getDbName(); if (dbName != null) { _db = mongoClient.getDB(dbName); } else { logger.error("MongoManager constructor failed. Database name is not provided."); throw new M2Exception("MongoManager constructor failed. Database name is not provided."); } } else { logger.error("MongoManager constructor failed. MongoClient is not provided."); throw new M2Exception("MongoManager constructor failed. MongoClient is not provided."); } } catch (MongoException ex) { logger.error("MongoManager constructor failed. Cannot connect to database.\n" + ex.toString()); throw new M2Exception("MongoManager constructor failed. Cannot connect to database."); } } else { logger.error("MongoManager constructor failed. Database connection object is null."); throw new M2Exception("MongoManager constructor failed. Database connection object is null."); } }
From source file:com.data.DAO.MetodoPagoDAO.java
public static String crearRecibo(Integer idMetodoPago, String descripcion, Integer cuotas, float precio, Integer idReserva) {//from w w w. j a v a 2s . c o m String reciboCreado = null; try { // To connect to mongo dbserver MongoClient mongoClient = new MongoClient("localhost", 27017); // Now connect to your databases DB db = mongoClient.getDB("hoex"); System.out.println("Connect to database successfully"); DBCollection coll = db.getCollection("Metodo_pago"); System.out.println("Collection Metodo_pago selected successfully"); BasicDBObject doc = new BasicDBObject("idMetodoPago", idMetodoPago).append("descripcion", descripcion) .append("cuotas", cuotas).append("precio", precio).append("idReserva", idReserva); coll.insert(doc); System.out.println("Document inserted successfully"); reciboCreado = "Success"; } catch (Exception e) { System.err.println(e.getClass().getName() + ": " + e.getMessage()); } if (reciboCreado != null) { return "El recibo fue creado satisfactoriamente!"; } else { return "El recibo no ha sido creado!"; } }
From source file:com.data.DAO.MetodoPagoDAO.java
public static ArrayList consultarRecibo(Integer idMetodoPago) { ArrayList reciboConsulta = new ArrayList(); try {//from w w w .j ava2 s . co m // To connect to mongo dbserver MongoClient mongoClient = new MongoClient("localhost", 27017); // Now connect to your databases DB db = mongoClient.getDB("hoex"); System.out.println("Connect to database successfully"); // Use an especific collection DBCollection coll = db.getCollection("Metodo_pago"); System.out.println("Collection Metodo_pago selected successfully"); BasicDBObject whereQuery = new BasicDBObject(); // Sentence to search one account number whereQuery.put("idMetodoPago", idMetodoPago); DBCursor cursor = coll.find(whereQuery); while (cursor.hasNext()) { DBObject consultDocument = cursor.next(); reciboConsulta.add(consultDocument.get("descripcion")); reciboConsulta.add(consultDocument.get("cuotas")); reciboConsulta.add(consultDocument.get("precio")); reciboConsulta.add(consultDocument.get("idReserva")); } System.out.println("Document consulted successfully"); } catch (Exception e) { System.err.println(e.getClass().getName() + ": " + e.getMessage()); } if (!reciboConsulta.isEmpty()) { return reciboConsulta; } else { return null; } }
From source file:com.data.DAO.MetodoPagoDAO.java
public static String actualizarRecibo(Integer idMetodoPago, String descripcion, Integer cuotas, float precio, Integer idReserva) {/*from w w w . j a va 2 s . c o m*/ String reciboActualizar = null; try { // To connect to mongo dbserver MongoClient mongoClient = new MongoClient("localhost", 27017); // Now connect to your databases DB db = mongoClient.getDB("hoex"); System.out.println("Connect to database successfully"); DBCollection coll = db.getCollection("Metodo_pago"); System.out.println("Collection Metodo_pago selected successfully"); BasicDBObject whereQuery = new BasicDBObject(); whereQuery.put("idMetodoPago", idMetodoPago); DBCursor cursor = coll.find(whereQuery); while (cursor.hasNext()) { DBObject updateDocument = cursor.next(); updateDocument.put("descripcion", descripcion); updateDocument.put("cuotas", cuotas); updateDocument.put("precio", precio); updateDocument.put("idReserva", idReserva); coll.update(whereQuery, updateDocument); } System.out.println("Document updated successfully"); reciboActualizar = "Success"; } catch (Exception e) { System.err.println(e.getClass().getName() + ": " + e.getMessage()); } if (reciboActualizar != null) { return "El recibo se ha actualizado correctamente!"; } else { return "El recibo no se ha actualizado!"; } }
From source file:com.data.DAO.MetodoPagoDAO.java
public static String borrarRecibo(Integer idMetodoPago) { String reciboBorrar = null;/* www.j a va 2s .co m*/ try { // To connect to mongo dbserver MongoClient mongoClient = new MongoClient("localhost", 27017); // Now connect to your databases DB db = mongoClient.getDB("hoex"); System.out.println("Connect to database successfully"); DBCollection coll = db.getCollection("Metodo_pago"); System.out.println("Collection Metodo_pago selected successfully"); BasicDBObject whereQuery = new BasicDBObject(); whereQuery.put("idMetodoPago", idMetodoPago); DBCursor cursor = coll.find(whereQuery); coll.remove(whereQuery); System.out.println("Documento elminado exitosamente"); reciboBorrar = "Success"; } catch (Exception e) { System.err.println(e.getClass().getName() + ": " + e.getMessage()); } if (reciboBorrar != null) { return "El recibo ha sido borrado exitosamente!"; } else { return "El recibo no ha sido borrado!"; } }
From source file:com.davidsalter.cappedcollection.JavaDriverCappedCollection.java
License:Apache License
/** * @param args the command line arguments *//*from ww w . ja v a2 s . com*/ public static void main(String[] args) throws UnknownHostException { MongoClient mongoClient = new MongoClient(new MongoClientURI("mongodb://localhost")); DB db = mongoClient.getDB("test"); DBCollection collection; if (!db.collectionExists("cappedLogsJavaDriver")) { BasicDBObject options = new BasicDBObject("capped", true); options.append("size", 4096); options.append("max", 5); collection = db.createCollection("cappedLogsJavaDriver", options); } else { collection = db.getCollection("cappedLogsJavaDriver"); } for (int i = 0; i < 8; i++) { BasicDBObject logEntry = new BasicDBObject("logId", i); collection.insert(logEntry); } }
From source file:com.davidsalter.cappedcollection.MorphiaCappedCollection.java
License:Apache License
/** * @param args the command line arguments */// w w w .java 2s .c om public static void main(String[] args) throws UnknownHostException { MongoClient mongoClient = new MongoClient(new MongoClientURI("mongodb://localhost")); DB db = mongoClient.getDB("test"); Morphia morphia = new Morphia(); morphia.map(LogEntry.class); Datastore datastore = morphia.createDatastore(mongoClient, "test"); datastore.ensureCaps(); for (int i = 0; i < 8; i++) { LogEntry logEntry = new LogEntry(i); datastore.save(logEntry); } }
From source file:com.deftlabs.lock.mongo.impl.BaseDao.java
License:Apache License
/** * Returns the db./*from w ww. ja v a2 s .c o m*/ */ protected final static DB getDb(final MongoClient pMongo, final DistributedLockSvcOptions pSvcOptions) { return pMongo.getDB(pSvcOptions.getDbName()); }