List of usage examples for com.mongodb DBCollection find
public DBCursor find(final DBObject query)
From source file:at.tuwien.aic.Main.java
private static void classifyTopic() { String topic = getNonEmptyString("Enter a topic you want to query"); Mongo mongo;/*from w w w . j a v a2 s . c om*/ DB db = null; try { mongo = new Mongo(_prop.getProperty("db_host")); db = mongo.getDB(_prop.getProperty("db_name")); } catch (UnknownHostException ex) { Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex); return; } Classifier c = ClassifyTweet.loadModel("resources/classifier.model"); TweetScorer scorer = new TweetScorer(); DBCollection tweetCollection = db.getCollection("tweets"); Pattern pattern = Pattern.compile("^.+" + topic + ".+$"); DBObject query = QueryBuilder.start("text").regex(pattern).get(); DBCursor resultSet = tweetCollection.find(query); int count = 0; double value = 0; double tweetClassifiedScore = 0; double tweetPosUserScore = 0; double tweetNegUserScore = 0; while (resultSet.hasNext()) { try { DBObject obj = resultSet.next(); String tweetText = (String) obj.get("text"); tweetClassifiedScore += ClassifyTweet.classifyTweet(c, tweetText); double score = scorer.scoreTweet(obj); if (tweetClassifiedScore > 0) { tweetPosUserScore += score; } else { tweetNegUserScore += score; } ++count; } catch (NumberFormatException ex) { Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex); } catch (JSONException ex) { Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex); } catch (IOException ex) { Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex); } } // Normalizing between 0 an 1 value = tweetPosUserScore / (tweetPosUserScore + tweetNegUserScore); System.out.println("This topic has a sentiment value of: " + value); }
From source file:br.bireme.scl.BrokenLinks.java
License:Open Source License
private static List<Integer> getIsisCcFields(final String mstName, final DBCollection coll) throws IOException { assert mstName != null; assert coll != null; final List<Integer> lst = new ArrayList<Integer>(); final BasicDBObject query = new BasicDBObject(MST_FIELD, mstName); final DBCursor cursor = coll.find(query); if (cursor.hasNext()) { final BasicDBObject obj = (BasicDBObject) cursor.next(); final BasicDBList flds = (BasicDBList) obj.get(CC_TAGS_FIELD); if (flds.isEmpty()) { throw new IOException("Missing CCs field"); }//w ww . j a va 2 s.c o m for (Object tag : flds) { lst.add((Integer) tag); } } else { throw new IOException("Missing collection: " + coll.getName()); } cursor.close(); return lst; }
From source file:br.bireme.scl.Gizmo.java
License:Open Source License
Collection<Element> loadRecords(final String host, final int port, final String user, final String password) throws IOException { assert host != null; assert port > 0; final MongoClient mongoClient = new MongoClient(host, port); final DB db = mongoClient.getDB(SOCIAL_CHECK_DB); if ((user != null) && (!user.trim().isEmpty())) { final boolean auth = db.authenticate(user, password.toCharArray()); if (!auth) { throw new IllegalArgumentException("invalid user/password"); }/*from ww w . ja va 2 s . c om*/ } final DBCollection coll = db.getCollection(HISTORY_COL); final String fldName = ELEM_LST_FIELD + ".0." + EXPORTED_FIELD; final BasicDBObject query = new BasicDBObject(fldName, false); final DBCursor cursor = coll.find(query); final Collection<Element> col = getNotExportedElements(coll, cursor); cursor.close(); return col; }
From source file:br.bireme.scl.MongoOperations.java
License:Open Source License
/** * Obtem uma lista com objetos IdUrl obtidos da base de dados MongoDb * @param coll coleo onde esto as urls//from w ww.j ava 2 s .c o m * @param centerIds filtro dos centros colaboradores desejados. Nunca nulo * @param filter se no nulo filtra as urls com um cc especfico * @param from indice inicial da lista a ser recuperado. Comea de 1. * @param count numero de elementos a serem devolvidos * @param ascendingOrder se retorna por ordem de data ascendente ou descendente * @return lista de objetos IdUrl */ private static List<IdUrl> getCenterUrls(final DBCollection coll, final Set<String> centerIds, final String filter, final int from, final int count, final boolean ascendingOrder) { if (coll == null) { throw new NullPointerException("coll"); } if (centerIds == null) { throw new NullPointerException("centerIds"); } if (centerIds.isEmpty()) { throw new IllegalArgumentException("empty centerIds"); } if (from < 1) { throw new IllegalArgumentException("from[" + from + "] < 1"); } if (count < 1) { throw new IllegalArgumentException("count[" + count + "] < 1"); } //final Set<IdUrl> lst = new TreeSet<IdUrl>(); final List<IdUrl> lst = new ArrayList<IdUrl>(); final SimpleDateFormat format = new SimpleDateFormat("dd-MM-yyyy HH:mm:ss"); final BasicDBObject sort = new BasicDBObject(DATE_FIELD, ascendingOrder ? 1 : -1); if (filter == null) { final BasicDBList cclst = new BasicDBList(); for (String centerId : centerIds) { cclst.add(centerId); } final BasicDBObject in = new BasicDBObject("$in", cclst); final BasicDBObject query = new BasicDBObject(CENTER_FIELD, in); //final DBCursor cursor = coll.find(query).skip(from - 1).limit(count); final DBCursor cursor = coll.find(query).sort(sort).skip(from - 1).limit(count); while (cursor.hasNext()) { final DBObject doc = cursor.next(); final BasicDBList ccsLst = (BasicDBList) doc.get(CENTER_FIELD); final Set<String> ccs = new TreeSet<String>(); for (Object cc : ccsLst) { ccs.add((String) cc); } final IdUrl iu = new IdUrl((String) doc.get(ID_FIELD), (String) doc.get(PRETTY_BROKEN_URL_FIELD), ccs, format.format((Date) (doc.get(DATE_FIELD))), (String) doc.get(MST_FIELD)); lst.add(iu); } cursor.close(); } else { final BasicDBObject query = new BasicDBObject(CENTER_FIELD, filter); final DBCursor cursor = coll.find(query).sort(sort).skip(from - 1).limit(count); while (cursor.hasNext()) { final DBObject doc = cursor.next(); final Set<String> ccs = new TreeSet<String>(); ccs.add(filter); final IdUrl iu = new IdUrl((String) doc.get(ID_FIELD), (String) doc.get(PRETTY_BROKEN_URL_FIELD), ccs, format.format((Date) doc.get(DATE_FIELD)), (String) doc.get(MST_FIELD)); lst.add(iu); } cursor.close(); } return lst; }
From source file:br.bireme.scl.MongoOperations.java
License:Open Source License
public static SearchResult getDocuments(final DBCollection coll, final String docMast, final String docId, final String docUrl, final Set<String> centerIds, final boolean decreasingOrder, final int from, final int count) { if (coll == null) { throw new NullPointerException("coll"); }/*from www.j av a 2 s. c o m*/ if (from < 1) { throw new IllegalArgumentException("from[" + from + "] < 1"); } if (count < 1) { throw new IllegalArgumentException("count[" + count + "] < 1"); } final List<IdUrl> lst = new ArrayList<IdUrl>(); final BasicDBObject query = new BasicDBObject(); if (docMast != null) { query.append(MST_FIELD, docMast); } if (docId != null) { final Pattern pat = Pattern.compile("^" + docId.trim() + "_\\d+"); query.append(ID_FIELD, pat); } if (docUrl != null) { query.append(BROKEN_URL_FIELD, docUrl.trim()); } if (centerIds != null) { final BasicDBList cclst = new BasicDBList(); for (String centerId : centerIds) { cclst.add(centerId); } final BasicDBObject in = new BasicDBObject("$in", cclst); query.append(CENTER_FIELD, in); } final BasicDBObject sort = new BasicDBObject(DATE_FIELD, decreasingOrder ? -1 : 1); final DBCursor cursor = coll.find(query).sort(sort).skip(from - 1).limit(count); final int size = cursor.count(); final SimpleDateFormat format = new SimpleDateFormat("dd-MM-yyyy"); while (cursor.hasNext()) { final DBObject doc = cursor.next(); final BasicDBList ccsLst = (BasicDBList) doc.get(CENTER_FIELD); final Set<String> ccs = new TreeSet<String>(); for (Object cc : ccsLst) { ccs.add((String) cc); } final IdUrl iu = new IdUrl((String) doc.get(ID_FIELD), (String) doc.get(PRETTY_BROKEN_URL_FIELD), ccs, format.format((Date) (doc.get(DATE_FIELD))), (String) doc.get(MST_FIELD)); lst.add(iu); } cursor.close(); return new SearchResult(size, lst); }
From source file:br.bireme.scl.MongoOperations.java
License:Open Source License
public static SearchResult2 getHistoryDocuments(final DBCollection coll, final Element elem, final int from, final int count) throws IOException, ParseException { if (coll == null) { throw new NullPointerException("coll"); }/* w ww. ja v a 2 s . co m*/ if (elem == null) { throw new NullPointerException("elem"); } if (from < 1) { throw new IllegalArgumentException("from[" + from + "] < 1"); } if (count < 1) { throw new IllegalArgumentException("count[" + count + "] < 1"); } final List<Element> lst = new ArrayList<Element>(); final BasicDBObject query = new BasicDBObject(); final String root = ELEM_LST_FIELD + ".0."; final String updated = root + LAST_UPDATE_FIELD; if (elem.getDbase() != null) { query.append(MST_FIELD, elem.getDbase().trim()); } if (elem.getId() != null) { final Pattern pat = Pattern.compile("^" + elem.getId().trim() + "_\\d+"); query.append(ID_FIELD, pat); } if (elem.getFurl() != null) { query.append(root + FIXED_URL_FIELD, elem.getFurl().trim()); } if (!elem.getCcs().isEmpty()) { final BasicDBList cclst = new BasicDBList(); for (String centerId : elem.getCcs()) { cclst.add(centerId.trim()); } final String cc = root + CENTER_FIELD; final BasicDBObject in = new BasicDBObject("$in", cclst); query.append(cc, in); } if (elem.getDate() != null) { final SimpleDateFormat simple = new SimpleDateFormat("dd-MM-yyyy"); final Date date = simple.parse(elem.getDate().trim()); final BasicDBObject qdate = new BasicDBObject("$gte", date); query.append(updated, qdate); } if (elem.getUser() != null) { final String user = root + USER_FIELD; query.append(user, elem.getUser().trim()); } final BasicDBObject sort = new BasicDBObject(updated, -1); final DBCursor cursor = coll.find(query).sort(sort).skip(from - 1).limit(count); final int size = cursor.count(); final SimpleDateFormat format = new SimpleDateFormat("dd-MM-yyyy"); while (cursor.hasNext()) { final BasicDBObject hdoc = (BasicDBObject) cursor.next(); final BasicDBList elst = (BasicDBList) hdoc.get(ELEM_LST_FIELD); final BasicDBObject hcurdoc = (BasicDBObject) elst.get(0); if (hcurdoc == null) { throw new IOException("document last element found."); } final BasicDBList ccLst = (BasicDBList) hcurdoc.get(CENTER_FIELD); final List<String> ccs = Arrays.asList(ccLst.toArray(new String[0])); final Element elem2 = new Element(hdoc.getString(ID_FIELD), hcurdoc.getString(BROKEN_URL_FIELD), hcurdoc.getString(PRETTY_BROKEN_URL_FIELD), hcurdoc.getString(FIXED_URL_FIELD), hdoc.getString(MST_FIELD), format.format((Date) (hcurdoc.get(LAST_UPDATE_FIELD))), hcurdoc.getString(USER_FIELD), ccs, hcurdoc.getBoolean(EXPORTED_FIELD)); lst.add(elem2); } cursor.close(); return new SearchResult2(size, lst.size(), lst); }
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 www. ja v a2 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.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 {//from w w w . j a v a2s. com 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 .j a v a 2s . c o m } cursor.close(); sendEmailToCc(ccp, eset); } } }
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 ww .j a va 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); }