List of usage examples for com.mongodb BasicDBObject BasicDBObject
public BasicDBObject(final String key, final Object value)
From source file:br.bireme.scl.ShowFixedLinks.java
License:Open Source License
public List<Element> showExportedLinks(final List<String> ccs, final String fromDate) throws ParseException { /*if (ccs == null) { throw new NullPointerException("ccs"); }*//*from w ww . java2s . c o m*/ final List<Element> lst = new ArrayList<Element>(); final SimpleDateFormat simple = new SimpleDateFormat("yyyyMMdd"); final SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yy"); final Date date = (fromDate == null) ? new Date(0) : simple.parse(fromDate); final String updated = ELEM_LST_FIELD + ".0." + LAST_UPDATE_FIELD; final BasicDBObject qdate = new BasicDBObject("$gte", date); final BasicDBObject query = new BasicDBObject(updated, qdate); final BasicDBObject sort = new BasicDBObject(updated, -1); final DBCursor cursor = coll.find(query).sort(sort); while (cursor.hasNext()) { final BasicDBObject doc = (BasicDBObject) cursor.next(); final BasicDBList elems = (BasicDBList) doc.get(ELEM_LST_FIELD); final BasicDBObject upd = (BasicDBObject) elems.get(0); final BasicDBList ccLst = (BasicDBList) upd.get(CENTER_FIELD); final List<String> ccs2 = new ArrayList<String>(); for (Object cc : ccLst) { ccs2.add((String) cc); } if (ccs == null) { final String id = doc.getString(ID_FIELD); final Element elem = new Element(id.substring(0, id.indexOf('_')), upd.getString(BROKEN_URL_FIELD), upd.getString(PRETTY_BROKEN_URL_FIELD), upd.getString(FIXED_URL_FIELD), doc.getString(MST_FIELD), sdf.format(upd.getDate(LAST_UPDATE_FIELD)), upd.getString(USER_FIELD), ccs2, upd.getBoolean(EXPORTED_FIELD)); lst.add(elem); } else { for (String cc : ccs) { if (ccLst.contains(cc)) { //System.out.println("cc=" + cc + " id=" + doc.getString(ID_FIELD)); final String id = doc.getString(ID_FIELD); final Element elem = new Element(id.substring(0, id.indexOf('_')), upd.getString(BROKEN_URL_FIELD), upd.getString(PRETTY_BROKEN_URL_FIELD), upd.getString(FIXED_URL_FIELD), doc.getString(MST_FIELD), sdf.format(upd.getDate(LAST_UPDATE_FIELD)), upd.getString(USER_FIELD), ccs2, upd.getBoolean(EXPORTED_FIELD)); lst.add(elem); break; } } } } cursor.close(); System.out.println("size=" + lst.size() + "\n"); return lst; }
From source file:br.bireme.scl.UndoUpdate.java
License:Open Source License
private static void undo(final String host, final int port, final String database, final String fromDate, final String docId) throws UnknownHostException, ParseException { assert host != null; assert port > 0; assert database != null; assert (fromDate != null || docId != null); final MongoClient client = new MongoClient(host, port); final DB db = client.getDB(database); final DBCollection from_coll = db.getCollection(HISTORY_COL); final DBCollection to_coll = db.getCollection(BROKEN_LINKS_COL); final String prefix = ELEM_LST_FIELD + ".0."; final BasicDBObject query; if (fromDate == null) { query = new BasicDBObject("_id", docId); } else {//from w w w.jav a 2s . c om final SimpleDateFormat simple = new SimpleDateFormat( "yyyyMMdd" + (fromDate.contains(":") ? "-HH:mm:ss" : "")); final Date fDate = simple.parse(fromDate); query = new BasicDBObject(prefix + LAST_UPDATE_FIELD, new BasicDBObject("$gte", fDate)); } final DBCursor cursor = from_coll.find(query); int total = 0; int reverted = 0; System.out.println("host=" + host); System.out.println("port=" + port); System.out.println("database=" + database); if (fromDate == null) { System.out.println("doc id=" + docId); } else { System.out.println("from date=" + fromDate); } System.out.println("found=" + cursor.size()); while (cursor.hasNext()) { final BasicDBObject doc_from = (BasicDBObject) cursor.next(); final BasicDBObject doc_to = new BasicDBObject(doc_from); final String id = doc_from.getString(ID_FIELD); final BasicDBList list = (BasicDBList) doc_from.get(ELEM_LST_FIELD); final BasicDBObject elem = (BasicDBObject) list.get(0); final Date date = elem.getDate(LAST_UPDATE_FIELD); doc_to.put(LAST_UPDATE_FIELD, date); doc_to.put(BROKEN_URL_FIELD, elem.getString(BROKEN_URL_FIELD)); doc_to.put(MSG_FIELD, elem.getString(MSG_FIELD)); doc_to.put(CENTER_FIELD, elem.get(CENTER_FIELD)); doc_to.removeField(ELEM_LST_FIELD); final WriteResult wr = to_coll.save(doc_to, WriteConcern.ACKNOWLEDGED); if (wr.getCachedLastError().ok()) { final WriteResult wr2 = from_coll.remove(doc_from, WriteConcern.ACKNOWLEDGED); if (wr2.getCachedLastError().ok()) { reverted++; System.out.println("+++id=" + id + " date=" + date); } else { System.err.println("Document[" + id + "] delete error."); } } else { System.err.println("Document[" + id + "] update error."); } total++; } cursor.close(); System.out.println("total/undo: " + total + "/" + reverted); }
From source file:br.com.esign.logistics.dao.impl.RoutesMapDAOImpl.java
License:Open Source License
@Override public void addRouteToMap(RoutesMap routesMap, Route route) { DBObject origin = new BasicDBObject("name", route.getOrigin().getName()); DBObject destination = new BasicDBObject("name", route.getDestination().getName()); DBObject routeToAdd = new BasicDBObject("name", route.getName()).append("name", route.getName()) .append("origin", origin).append("destination", destination).append("distance", route.getDistance()) .append("slug", route.getSlug()); DBObject routes = new BasicDBObject("routes", routeToAdd); DBObject routesMapToUpdate = new BasicDBObject(Mapper.ID_KEY, routesMap.getName()); DBObject pushOperation = new BasicDBObject("$push", routes); getCollection().update(routesMapToUpdate, pushOperation); }
From source file:br.com.esign.logistics.dao.impl.RoutesMapDAOImpl.java
License:Open Source License
@Override public void removeRouteFromMap(RoutesMap routesMap, Route route) { DBObject routeToRemove = new BasicDBObject("name", route.getName()); DBObject routes = new BasicDBObject("routes", routeToRemove); DBObject routesMapToUpdate = new BasicDBObject(Mapper.ID_KEY, routesMap.getName()); DBObject pullOperation = new BasicDBObject("$pull", routes); getCollection().update(routesMapToUpdate, pullOperation); }
From source file:br.com.ezequieljuliano.argos.persistence.LoggerDAO.java
License:Apache License
@PostConstruct public void ensureFullTextIndex() { // make sure the index is set up properly (not yet possible via Spring Data Annotations) getMongoOperations().getCollection(Logger.COLLECTION_NAME) .ensureIndex(new BasicDBObject("fullText", "text")); }
From source file:br.com.ifspsaocarlos.gastock.library.Mongodb.java
public List<BasicDBObject> buscaGeral() { DBCursor cursor = collection.find().sort(new BasicDBObject("cod", 1)); List<BasicDBObject> resultado = new ArrayList<>(); while (cursor.hasNext()) { resultado.add((BasicDBObject) cursor.next()); }/*from w w w . jav a 2 s . com*/ return resultado; }
From source file:br.com.ifspsaocarlos.gastock.library.Mongodb.java
public boolean alterarItem(int codigo, BasicDBObject item) { BasicDBObject query = new BasicDBObject("cod", codigo); try {// w w w . ja v a2 s. c o m this.collection.update(query, item); } catch (Exception ex) { return false; } return true; }
From source file:br.com.ifspsaocarlos.gastock.models.MCombustivel.java
@Override public Combustivel get(int combustivelId) throws Exception { DBObject dados = this.banco.buscaRegistro(new BasicDBObject("cod", combustivelId)); Combustivel result = new Combustivel((int) dados.get("cod"), dados.get("nome").toString(), Double.parseDouble(dados.get("preco").toString())); return result; }
From source file:br.com.ifspsaocarlos.gastock.models.MFrentista.java
@Override public Frentista get(int frentistaId) throws Exception { DBObject dados = this.banco.buscaRegistro(new BasicDBObject("cod", frentistaId)); Frentista result = new Frentista((int) dados.get("cod"), dados.get("nome").toString(), dados.get("senha").toString(), dados.get("tipo").toString(), Double.parseDouble(dados.get("salario").toString())); return result; }
From source file:br.com.ifspsaocarlos.gastock.models.MRelatorio.java
@Override public Relatorio get(int relatorioId) throws Exception { DBObject dados = this.banco.buscaRegistro(new BasicDBObject("cod", relatorioId)); Relatorio result = new Relatorio((int) dados.get("cod"), dados.get("dataAtual").toString(), dados.get("combustivel").toString(), (int) dados.get("bomba"), Double.parseDouble(dados.get("quantidade").toString()), Double.parseDouble(dados.get("precoTotal").toString())); return result; }