List of usage examples for com.mongodb BasicDBObject get
public Object get(final String key)
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 ww w. ja v a2s.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.ShowBrokenLinks.java
License:Open Source License
public Map<String, String> statistics() throws IOException { final Map<String, String> ret = new TreeMap<String, String>(); final Map<String, Integer> map = new HashMap<String, Integer>(); final DBCursor cursor = coll.find(); for (DBObject obj : cursor) { final BasicDBObject dobj = (BasicDBObject) obj; final BasicDBList ccList = (BasicDBList) dobj.get(CENTER_FIELD); final String cc = (String) ccList.get(0); if (cc == null) { throw new IOException("null center field. Id=" + dobj.getString(ID_FIELD)); }// w w w .j ava2s . c om Integer val = map.get(cc); if (val == null) { val = 0; } map.put(cc, val + 1); } cursor.close(); for (Map.Entry<String, Integer> entry : map.entrySet()) { final String key = entry.getKey(); final int value = entry.getValue(); final int dif = 999999 - value; ret.put(dif + "_" + value + "_" + key, key); } return ret; }
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 . ja v a2s .co 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 ww. j a va 2s . 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 2 s. 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.ezequieljuliano.argos.template.StandardDAO.java
License:Apache License
private Collection<ObjectId> extractSearchResultIds(CommandResult commandResult) { Set<ObjectId> objectIds = new HashSet<ObjectId>(); BasicDBList resultList = (BasicDBList) commandResult.get("results"); if (resultList == null) { System.out.println(mongoFullTextSearchErrorMessage()); return objectIds; }/*from w w w .j a v a2 s . co m*/ Iterator<Object> it = resultList.iterator(); while (it.hasNext()) { BasicDBObject resultContainer = (BasicDBObject) it.next(); BasicDBObject resultObject = (BasicDBObject) resultContainer.get("obj"); ObjectId resultId = (ObjectId) resultObject.get("_id"); objectIds.add(resultId); } return objectIds; }
From source file:br.ufabc.impress.mongo.manager.DBHelper.java
@Override public String addByMap(String tableName, Map columns) { if (tableName == null || tableName.equals("") || columns == null || columns.equals("")) { return "501"; }/*from w w w . j a va 2s. com*/ DBCollection table = db.getCollection(tableName); BasicDBObject document = new BasicDBObject(columns); WriteResult wRes = table.insert(document); return ((ObjectId) document.get("_id")).toString(); }
From source file:br.ufabc.impress.mongo.manager.DBHelper.java
@Override public String addDefaultMap(String tableName, Map columns) { if (tableName == null || tableName.equals("") || columns == null || columns.equals("")) { return "501"; }/*www .j a v a2s. com*/ columns.put("status", "active"); columns.put("createDate", System.currentTimeMillis() + ""); columns.put("updateDate", System.currentTimeMillis() + ""); DBCollection table = db.getCollection(tableName); BasicDBObject document = new BasicDBObject(columns); WriteResult wRes = table.insert(document); return ((ObjectId) document.get("_id")).toString(); }
From source file:brooklyn.entity.nosql.mongodb.MongoDBTestHelper.java
License:Apache License
/** * Inserts a new object with { key: value } at given server. * @return The new document's id// w w w. j ava2s. c om */ public static String insert(AbstractMongoDBServer entity, String key, Object value) { LOG.info("Inserting {}:{} at {}", new Object[] { key, value, entity }); MongoClient mongoClient = clientForServer(entity); try { DB db = mongoClient.getDB(TEST_DB); DBCollection testCollection = db.getCollection(TEST_COLLECTION); BasicDBObject doc = new BasicDBObject(key, value); testCollection.insert(doc); ObjectId id = (ObjectId) doc.get("_id"); return id.toString(); } finally { mongoClient.close(); } }
From source file:ch.agent.crnickl.mongodb.AccessMethodsForNumber.java
License:Apache License
private Observation<Double> extractLastValue(BasicDBObject obj, TimeDomain domain, TimeIndex t) throws Exception { long first = obj.getLong(MongoDatabase.FLD_SER_FIRST); long last = obj.getLong(MongoDatabase.FLD_SER_LAST); Observation<Double> obs = null; long time = t == null ? last : t.asLong(); if (last >= first && time >= first) { @SuppressWarnings("unchecked") Map<String, Double> values = (Map<String, Double>) obj.get(MongoDatabase.FLD_SER_VALUES); String key = null;// w w w. j a v a 2 s. c o m if (time >= last) key = Long.toString(last); else { TreeMap<String, Double> sorted = new TreeMap<String, Double>(values); key = sorted.headMap(Long.toString(time + 1)).lastKey(); } obs = new Observation<Double>(domain.time(Long.valueOf(key)), values.get(key)); } return obs; }