List of usage examples for com.mongodb DBCursor next
@Override
public DBObject next()
From source file:br.bireme.scl.MongoOperations.java
License:Open Source License
public static boolean undoUpdateDocument2(final DBCollection coll, final DBCollection hcoll, final String fromDate) throws IOException, ParseException { if (coll == null) { throw new NullPointerException("coll"); }//from w ww . j a va2 s.c o m if (hcoll == null) { throw new NullPointerException("hcoll"); } final SimpleDateFormat simple = new SimpleDateFormat("yyyyMMdd"); 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); boolean ret = true; while (cursor.hasNext()) { final BasicDBObject hdoc = (BasicDBObject) cursor.next(); final BasicDBList lst = (BasicDBList) hdoc.get(ELEM_LST_FIELD); final BasicDBObject hcurdoc = (BasicDBObject) lst.remove(0); if (hcurdoc == null) { throw new IOException("document last element found."); } final BasicDBObject doc = new BasicDBObject(); doc.put(DATE_FIELD, hdoc.get(DATE_FIELD)); doc.put(LAST_UPDATE_FIELD, hcurdoc.get(LAST_UPDATE_FIELD)); doc.put(MST_FIELD, hdoc.get(MST_FIELD)); doc.put(ID_FIELD, hdoc.get(ID_FIELD)); doc.put(BROKEN_URL_FIELD, hcurdoc.get(BROKEN_URL_FIELD)); doc.put(PRETTY_BROKEN_URL_FIELD, hcurdoc.get(PRETTY_BROKEN_URL_FIELD)); doc.put(MSG_FIELD, hcurdoc.get(MSG_FIELD)); doc.put(CENTER_FIELD, hcurdoc.get(CENTER_FIELD)); final boolean ret1 = coll.save(doc).getLastError().ok(); final boolean ret2; if (lst.isEmpty()) { ret2 = hcoll.remove(query, WriteConcern.ACKNOWLEDGED).getLastError().ok(); } else { ret2 = hcoll.save(hdoc, WriteConcern.ACKNOWLEDGED).getLastError().ok(); } final boolean auxret = (ret1 && ret2); if (!auxret) { System.err.println("doc[" + hdoc.get(ID_FIELD) + "] write error"); } ret &= auxret; } return ret; }
From source file:br.bireme.scl.MongoOperations.java
License:Open Source License
public static void fixMissingHttp(final DBCollection coll, final DBCollection hcoll) throws IOException { if (coll == null) { throw new NullPointerException("coll"); }/*w ww.j a v a2 s . c o m*/ if (hcoll == null) { throw new NullPointerException("hcoll"); } final String HTTP = "http://"; final DBCursor cursor = coll.find(); while (cursor.hasNext()) { final DBObject dbo = cursor.next(); final String url = ((String) dbo.get(BROKEN_URL_FIELD)).trim(); if (!url.startsWith(HTTP)) { final String fixedUrl = HTTP + url; final String id = (String) dbo.get(ID_FIELD); if (!CheckUrl.isBroken(CheckUrl.check(fixedUrl))) { if (!Tools.isDomain(fixedUrl)) { if (!updateDocument(coll, hcoll, id, fixedUrl, "system", true)) { throw new IOException("could not update document id=" + id); } } } } } cursor.close(); }
From source file:br.bireme.scl.ResetExportFlag.java
License:Open Source License
private static void reset(final String host, final int port, final String database, final String collection, final String sdate) throws UnknownHostException, ParseException { assert host != null; assert port > 0; assert database != null; assert collection != null; final MongoClient client = new MongoClient(host, port); final DB db = client.getDB(database); final DBCollection coll = db.getCollection(collection); final String prefix = ELEM_LST_FIELD + ".0."; final BasicDBObject query; final DBCursor cursor; if (sdate == null) { query = new BasicDBObject(prefix + EXPORTED_FIELD, true); } else {// w w w . j a v a 2s . c om final SimpleDateFormat simple = new SimpleDateFormat("yyyyMMdd"); final Date date = simple.parse(sdate); final BasicDBList list = new BasicDBList(); list.add(new BasicDBObject(prefix + EXPORTED_FIELD, true)); list.add(new BasicDBObject(prefix + LAST_UPDATE_FIELD, new BasicDBObject("$gte", date))); query = new BasicDBObject("$and", list); } cursor = coll.find(query); while (cursor.hasNext()) { final BasicDBObject doc = (BasicDBObject) cursor.next(); final BasicDBList list = (BasicDBList) doc.get(ELEM_LST_FIELD); final BasicDBObject elem = (BasicDBObject) list.get(0); elem.put(EXPORTED_FIELD, false); coll.save(doc); } cursor.close(); }
From source file:br.bireme.scl.SendEmailToCcs.java
License:Open Source License
private void prepareEmails(final DBCollection coll, final Map<String, CcProfile> profiles) { assert coll != null; assert profiles != null; final Set<String> ccs = MongoOperations.getCenters(coll); final String qry = CENTER_FIELD + ".0"; for (String cc : ccs) { final Set<EmailFrame> eset = new HashSet<EmailFrame>(); final CcProfile ccp = profiles.get(cc); if (ccp != null) { final BasicDBObject query = new BasicDBObject(qry, cc); final DBCursor cursor = coll.find(query); while (cursor.hasNext()) { final BasicDBObject doc = (BasicDBObject) cursor.next(); final String id = doc.getString(ID_FIELD); final EmailFrame eframe = new EmailFrame(id.substring(0, id.indexOf('_')), doc.getString(MST_FIELD), doc.getString(BROKEN_URL_FIELD)); eset.add(eframe);//from w w w . ja v a 2s . co m } cursor.close(); sendEmailToCc(ccp, eset); } } }
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"); }*/// ww w . j a v a 2 s. 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.TabulateField.java
License:Open Source License
public static void tabulate(final String host, final int port, final String user, final String password, final String database, final String collection, final String path) throws UnknownHostException { if (host == null) { throw new NullPointerException("host"); }//from w w w . ja v a 2 s .com if (port <= 0) { throw new IllegalArgumentException("port <= 0"); } if (collection == null) { throw new NullPointerException("collection"); } if (path == null) { throw new NullPointerException("path"); } final MongoClient mongoClient = new MongoClient(host, port); final DB db = mongoClient.getDB(database); if (user != null) { final boolean auth = db.authenticate(user, password.toCharArray()); if (!auth) { throw new IllegalArgumentException("invalid user/password"); } } final DBCollection coll = db.getCollection(collection); final DBCursor cursor = coll.find(); final TreeMap<String, Integer> map1 = new TreeMap<String, Integer>(); final TreeMap<Integer, String> map2 = new TreeMap<Integer, String>(); final int ADD_VALUE = 99999999; final int size = cursor.size(); while (cursor.hasNext()) { final BasicDBObject doc = (BasicDBObject) cursor.next(); final String key = getValue(doc, path); if (key != null) { Integer val = map1.get(key); if (val == null) { val = 0; } val += 1; map1.put(key, val); } } cursor.close(); for (Map.Entry<String, Integer> entry : map1.entrySet()) { map2.put(ADD_VALUE - entry.getValue(), entry.getKey()); } System.out.println("Total # of docs: " + size + "\n"); int idx = 0; for (Map.Entry<Integer, String> entry : map2.entrySet()) { final int val = ADD_VALUE - entry.getKey(); final String percent = String.format("%.2f", ((float) val / size) * 100); System.out.println((++idx) + ") " + entry.getValue() + ": " + val + " (" + percent + "%)"); } }
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 .java 2 s . c o m*/ 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.bireme.tmp.AddPrettyField.java
License:Open Source License
private static void add(final String mongo_host, final int mongo_port, final String mongo_db, final String mongo_col) throws UnknownHostException { assert mongo_host != null; assert mongo_port > 0; assert mongo_db != null; assert mongo_col != null; final MongoClient client = new MongoClient(mongo_host, mongo_port); final DB db = client.getDB(mongo_db); final DBCollection coll = db.getCollection(HISTORY_COL); final DBCursor cursor = coll.find(); int total = 0; System.out.println("host=" + mongo_host); System.out.println("port=" + mongo_port); System.out.println("database=" + mongo_db); System.out.println("collection=" + mongo_col); System.out.println("num of documents=" + cursor.size()); while (cursor.hasNext()) { final BasicDBObject doc1 = (BasicDBObject) cursor.next(); final BasicDBList list = (BasicDBList) doc1.get(ELEM_LST_FIELD); for (Object obj : list) { final BasicDBObject doc2 = (BasicDBObject) obj; if (!doc2.containsField(PRETTY_BROKEN_URL_FIELD)) { final String burl = doc2.getString(BROKEN_URL_FIELD); try { final String pburl = EncDecUrl.decodeUrl(burl); doc2.append(PRETTY_BROKEN_URL_FIELD, pburl); final WriteResult wr = coll.save(doc1, WriteConcern.ACKNOWLEDGED); if (wr.getCachedLastError().ok()) { total++;// ww w . j a v a 2s .co m } else { System.err.println("Document[" + doc1.getString("_id") + "] update error."); } } catch (IOException ioe) { System.err.println("Document[" + doc1.getString("_id") + "] bad encode conversion" + " url=[" + burl + "]"); } } } } cursor.close(); System.out.println("num of added fields: " + total); }
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 ww w . j ava 2 s . c o m return resultado; }
From source file:br.com.rbezerra.RegisterQueue.DAO.DefaultRegistroDAO.java
public List<Registro> listar() { List<Registro> atendimentos = new ArrayList<Registro>(); DBCursor cursor = collectionReg.find(); while (cursor.hasNext()) { atendimentos.add((Registro) cursor.next()); }//from www. j a v a 2 s . c om return atendimentos; }