List of usage examples for com.mongodb MongoClient close
public void close()
From source file:com.zns.vehicles.service.dao.impl.VehicleDAOImpl.java
private ArrayList<String> dbGetUtil(RetrievalRequest requestPartial) { MongoClient mongoClient = null; ArrayList<String> results = new ArrayList<String>(); try {/*ww w . j a va 2 s . co m*/ mongoClient = new MongoClient(props.getProperty(HOST), PORT); MongoDatabase db = mongoClient.getDatabase("vehicleApp"); log.info("querying this collection >>>>>>>>>>>>>>>>>>>>>>>>>> " + db.getCollection("vehicles").toString()); log.info("looking for vehicle type:::::::: " + requestPartial.getVehicleType()); FindIterable<Document> iterable = db.getCollection("vehicles") .find(new Document("username", requestPartial.getUserName()).append("vehicleType", requestPartial.getVehicleType())); iterable.forEach(new Block<Document>() { @Override public void apply(final Document document) { log.info("result>>>>>>>>>>>>>> " + document.toString()); results.add(document.toJson()); } }); } catch (MongoClientException e) { log.error("Error while connecting to DB..."); e.printStackTrace(); } catch (MongoException e) { log.error("General mongo error"); e.printStackTrace(); } catch (IllegalArgumentException e) { log.error("Illegal argument exception..."); e.printStackTrace(); } finally { mongoClient.close(); } log.info("arraylist contents for search::::::::: " + results.size()); return results; }
From source file:course.homework.Homework3.java
License:Apache License
public static void main(String[] args) { MongoClient client = new MongoClient(); MongoDatabase database = client.getDatabase("school"); MongoCollection<Document> collection = database.getCollection("students"); MongoCursor<Document> iterator = collection.find().iterator(); while (iterator.hasNext()) { Document doc = iterator.next(); List<Document> scores = (List<Document>) doc.get("scores"); Optional<Document> bestHomeWork = scores.stream() .max((a, b) -> a.getString("type").equals("homework") && b.getString("type").equals("homework") ? Double.compare(a.getDouble("score"), b.getDouble("score")) : -1);/* w ww.ja va 2 s.c om*/ Double bestScore = bestHomeWork.get().getDouble("score"); List<Document> result = scores.stream() .filter(x -> !x.getString("type").equals("homework") || x.getDouble("score").equals(bestScore)) .collect(Collectors.toList()); collection.updateOne(eq("_id", doc.get("_id")), new Document("$set", new Document("scores", result))); } iterator.close(); client.close(); }
From source file:dao.CocinaDAO.java
public List<Mensaje> getMensajes() { int estado;//from ww w . j av a 2 s. c o m List<Mensaje> mensajes = null; Mensaje mensaje = null; ConexionMLab con = new ConexionMLab(); MongoClient mongo = con.getConexion(); try { DB db = mongo.getDB("pizzaplaneta"); DBCollection coleccion = db.getCollection("pedido"); DBCursor cursor = coleccion.find(); mensajes = new ArrayList<>(); while (cursor.hasNext()) { DBObject dbo = cursor.next(); BasicDBList dbo1 = (BasicDBList) dbo.get("estados"); DBObject est = (DBObject) (dbo1.get(dbo1.size() - 1)); estado = (Integer) est.get("id"); if (estado == 1 || estado == 0) { mensaje = new Mensaje((String) est.get("fechaHora"), estado, (Integer) dbo.get("_id")); mensajes.add(mensaje); } } } catch (Exception e) { e.printStackTrace(); } finally { mongo.close(); } return mensajes; }
From source file:dao.CocinaDAO.java
public List<Pizza> getPizza(int id) { ConexionMLab con = new ConexionMLab(); MongoClient mongo = con.getConexion(); try {//from w w w . j a v a 2 s .c o m DB db = mongo.getDB("pizzaplaneta"); DBCollection coleccion = db.getCollection("pedido"); BasicDBObject query = new BasicDBObject(); query.put("_id", id); DBCursor cursor = coleccion.find(query); pizzas = new ArrayList<>(); if (cursor.hasNext()) { DBObject dbo = cursor.next(); //System.out.println("si " + (String)dbo.get("username")); BasicDBList dbo1 = (BasicDBList) ((DBObject) dbo.get("productos")).get("promociones"); if (dbo1 != null) { for (Object promo : dbo1) { DBObject dbo3 = DBObject.class.cast(promo); BasicDBList dboPi = (BasicDBList) dbo3.get("pizzas"); for (Object pizza : dboPi) { recorrer(DBObject.class.cast(pizza)); } } } BasicDBList dboP1 = (BasicDBList) ((DBObject) dbo.get("productos")).get("pizzas"); if (dboP1 != null) { for (Object pizza : dboP1) { recorrer(DBObject.class.cast(pizza)); } } } } catch (Exception e) { e.printStackTrace(); } finally { mongo.close(); } return pizzas; }
From source file:dao.CocinaDAO.java
void Eingredientes(String nombre) { ConexionMLab con = new ConexionMLab(); MongoClient mongo = con.getConexion(); try {/* w w w . j a va 2 s . co m*/ DB db = mongo.getDB("pizzaplaneta"); DBCollection coleccion = db.getCollection("pizza"); BasicDBObject query = new BasicDBObject(); query.put("nombre", nombre); ingredientes = new ArrayList<>(); DBCursor cursor = coleccion.find(query); if (cursor.hasNext()) { DBObject dbo = cursor.next(); BasicDBList dbo2 = (BasicDBList) dbo.get("ingredientes"); if (dbo2 != null) { for (Object ing : dbo2) { DBObject dbo5 = (DBObject) ing; ingred = new Ingrediente((Integer) dbo5.get("id"), (String) dbo5.get("nombre")); ingredientes.add(ingred); } } } } catch (Exception e) { e.printStackTrace(); } finally { mongo.close(); } }
From source file:dao.CocinaDAO.java
public Integer actualizarEstado(int idPedido, String username) { int idestado; ConexionMLab con = new ConexionMLab(); MongoClient mongo = con.getConexion(); DateFormat df = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss"); Date fecha = new Date(); int fields = 0; try {//from www .j a v a 2 s .c o m DB db = mongo.getDB("pizzaplaneta"); DBCollection coleccion = db.getCollection("pedido"); BasicDBObject query = new BasicDBObject(); query.put("_id", idPedido); DBCursor cursor = coleccion.find(query); DBObject dbo = cursor.next(); BasicDBList dbo1 = (BasicDBList) dbo.get("estados"); idestado = (Integer) ((DBObject) (dbo1.get(dbo1.size() - 1))).get("id"); gEstado = new Estado(); switch (idestado + 1) { case 1: gEstado.setId(1); gEstado.setFechaHora(df.format(fecha)); gEstado.setUsername(username); break; case 2: gEstado.setId(2); gEstado.setFechaHora(df.format(fecha)); gEstado.setUsername(username); agregarPedidoDistribuidor(2); break; case 3: gEstado.setId(2); gEstado.setFechaHora(df.format(fecha)); gEstado.setUsername(username); agregarPedidoDistribuidor(2); break; } DBObject dbo3 = new BasicDBObject(); dbo3.put("id", gEstado.getId()); dbo3.put("fechaHora", gEstado.getFechaHora()); dbo3.put("username", gEstado.getUsername()); DBObject dbo4 = new BasicDBObject(); dbo4.put("estados", dbo3); DBObject dbo5 = new BasicDBObject(); dbo5.put("$push", dbo4); coleccion.update(query, dbo5).getN(); fields = idestado + 1; } catch (Exception e) { e.printStackTrace(); } finally { mongo.close(); } return fields; }
From source file:dao.CocinaDAO.java
void agregarPedidoDistribuidor(int idPedido) { ConexionMLab con = new ConexionMLab(); MongoClient mongo = con.getConexion(); try {//ww w . ja v a2s .c o m DB db = mongo.getDB("pizzaplaneta"); DBCollection coleccion = db.getCollection("empleado"); BasicDBObject query = new BasicDBObject(); BasicDBObject query1 = new BasicDBObject(); query1.put("$eq", "dtorres"); query.put("username", query1); DBObject dbo3 = new BasicDBObject(); dbo3.put("id", idPedido); DBObject dbo4 = new BasicDBObject(); dbo4.put("pedidos", dbo3); DBObject dbo5 = new BasicDBObject(); dbo5.put("$push", dbo4); coleccion.update(query, dbo5).getN(); } catch (Exception e) { e.printStackTrace(); } finally { mongo.close(); } }
From source file:dao.CocinaDAO.java
public Integer login(String usuario, String password) { ConexionMLab con = new ConexionMLab(); MongoClient mongo = con.getConexion(); int variable = 0; try {//from w w w .j av a2 s. c o m DB db = mongo.getDB("pizzaplaneta"); DBCollection coleccion = db.getCollection("usuario"); BasicDBObject query = new BasicDBObject(); query.put("username", usuario); query.put("password", password); DBCursor cursor = coleccion.find(query); if (cursor.hasNext()) { variable = 1; } } catch (Exception e) { e.printStackTrace(); } finally { mongo.close(); } return variable; }
From source file:dao.DaoDocumentos.java
public boolean salvarCompra(Compra compra) { MongoClient client = new MongoClient("localhost", 27017); MongoDatabase dataBase = client.getDatabase("bdnc-loja"); MongoCollection<Document> collection = dataBase.getCollection("vendas"); collection.insertOne(compra.toDocument()); client.close(); return true;/*w w w . j av a 2 s . co m*/ }
From source file:dao.DaoDocumentos.java
public List<Compra> buscarCompras() { MongoClient client = new MongoClient("localhost", 27017); MongoDatabase dataBase = client.getDatabase("bdnc-loja"); MongoCollection<Document> collection = dataBase.getCollection("vendas"); List<Compra> compras = new ArrayList<>(); MongoCursor<Document> cursor = collection.find().iterator(); while (cursor.hasNext()) { Compra compra = new Compra(); compras.add(compra.convertFromDocument(cursor.next())); }//from w w w .j av a 2 s. c om client.close(); return compras; }