List of usage examples for com.mongodb DBObject get
Object get(String key);
From source file:be.solidx.hot.data.mongo.DBObjectNativeObjectTransformer.java
License:Open Source License
public static NativeObject dbObjectToNativeObject(DBObject dbObject) { NativeObject nativeObject = new NativeObject(); if (dbObject == null) { return null; }/*from www . j a v a2s . co m*/ for (String key : dbObject.keySet()) { if (dbObject.get(key) instanceof List<?>) { nativeObject.put(key.toString(), nativeObject, listToNativeArray((List<?>) dbObject.get(key))); } else if (dbObject.get(key) instanceof DBObject) { nativeObject.put(key.toString(), nativeObject, dbObjectToNativeObject((DBObject) dbObject.get(key))); } else { nativeObject.put(key.toString(), nativeObject, key.equals("_id") ? dbObject.get(key).toString() : dbObject.get(key)); } } return nativeObject; }
From source file:be.solidx.hot.data.mongo.DBObjectPyDictionaryTransformer.java
License:Open Source License
public PyDictionary dbObjectToPyDictionary(DBObject dbObject) { if (dbObject == null) { return null; }/*from w w w. j a v a 2 s. c om*/ PyDictionary pyDictionary = new PyDictionary(); for (String key : dbObject.keySet()) { if (dbObject.get(key) instanceof List<?>) { pyDictionary.put(key.toString(), transformList((List<?>) dbObject.get(key))); } else if (dbObject.get(key) instanceof DBObject) { pyDictionary.put(key.toString(), dbObjectToPyDictionary((DBObject) dbObject.get(key))); } else { pyDictionary.put(key.toString(), key.equals("_id") ? dbObject.get(key).toString() : dbObject.get(key)); } } return pyDictionary; }
From source file:bhl.pages.database.MongoConnection.java
License:Open Source License
/** * Make a subset of the documents by a given subkey and value, * then retrieve all the values of the field * @param collection the collection to search * @param subKey the subKey to make the initial choice * @param subValue the value of the subKey to search for * @param fields the field names to retrieve * @return and array of field values as JSON object strings * @throws DbException //w ww .j a va2 s.co m */ @Override public String[] listCollectionBySubKey(String collection, String subKey, String subValue, String[] fields) throws DbException { try { connect(); DBCollection coll = getCollectionFromName(collection); DBObject query = new BasicDBObject(subKey, subValue); BasicDBObject keys = new BasicDBObject(); for (int i = 0; i < fields.length; i++) keys.put(fields[i], 1); DBCursor cursor = coll.find(query, keys); if (cursor.length() > 0) { String[] array = new String[cursor.length()]; Iterator iter = cursor.iterator(); int i = 0; while (iter.hasNext()) { DBObject bson = (DBObject) iter.next(); JSONObject jobj = new JSONObject(); for (int j = 0; j < fields.length; j++) jobj.put(fields[j], bson.get(fields[j])); array[i++] = jobj.toJSONString(); } return array; } else return new String[0]; } catch (Exception e) { throw new DbException(e); } }
From source file:bhl.pages.database.MongoConnection.java
License:Open Source License
/** * Get the content of the given page and document * @param docid the document identifier (an integer in BHL) * @param pageid the page identifier (NOT page_sequence) also an int * @return the textual content as a String * @throws Exception /*w w w . ja v a 2s . c o m*/ */ public String getPageContent(String docid, String pageid) throws DbException { try { if (docid != null && pageid != null) { connect(); DBCollection coll = db.getCollection(Database.PAGES); BasicDBObject ref = new BasicDBObject(); ref.put(JSONKeys.IA_IDENTIFIER, docid); ref.put(JSONKeys.BHL_PAGE_ID, Integer.parseInt(pageid)); BasicDBObject key = new BasicDBObject(); key.put(JSONKeys.PAGE_SEQUENCE, 1); DBObject obj = coll.findOne(ref, key); if (obj != null) { Object pobj = obj.get(JSONKeys.PAGE_SEQUENCE); int pageNo = ((Number) pobj).intValue(); DBCollection coll2 = db.getCollection(Database.DOCUMENTS); BasicDBObject ref2 = new BasicDBObject(); ref2.put(JSONKeys.IA_IDENTIFIER, docid); ref2.put(JSONKeys.PAGE_SEQUENCE, pageNo); BasicDBObject key2 = new BasicDBObject(); key2.put(JSONKeys.CONTENT, 1); Object obj2 = coll2.findOne(ref2, key2); if (obj2 != null) return (String) ((DBObject) obj2).get(JSONKeys.CONTENT); else throw new Exception("could not find content for docid=" + docid + ", pageid=" + pageid); } else throw new Exception("could not find docid=" + docid + ", pageid=" + pageid); } else throw new Exception("Missing docid or pageid"); } catch (Exception e) { throw new DbException(e); } }
From source file:biopolis.headless.BiopolisSegmentationManagement.java
public List<Long> search(BiopolisSegmentQuery seg) throws BiopolisGeneralException { byte[] bytes = Base64.decodeBase64(seg.biopolisid); ObjectId someid = new ObjectId(bytes); DBCursor cur = dbcol.find(new BasicDBObject("_id", someid)); if (!cur.hasNext()) { throw new BiopolisGeneralException("Search not exists with id " + seg.biopolisid); }// ww w .java2s . c o m BasicDBObject setter = new BasicDBObject("$set", new BasicDBObject("biopolisttl", new java.util.Date())); dbcol.update(new BasicDBObject("_id", someid), setter); dbcol.update(new BasicDBObject("biopolisid", seg.biopolisid), setter); List<Long> resus = new ArrayList<Long>(); cur = dbcol.find(new BasicDBObject("biopolisid", seg.biopolisid)); cur = cur.sort(new BasicDBObject("biopolisdata", 1)); int count = 0; while (cur.hasNext()) { System.out.println("scan " + seg.from); DBObject obj = cur.next(); if ((count >= seg.from) && (count < seg.from + this.segSZ)) { System.out.println("add"); Long i = (Long) obj.get("biopolisdata"); resus.add(i); } count++; } System.out.println(resus.size()); return resus; }
From source file:br.bireme.scl.CopyMongoDb.java
License:Open Source License
private static void copyDB(final String from_host, final String to_host, final String from_db, final String to_db, final String from_port, final String to_port, final boolean appendCollections, final boolean displayAllIds) throws UnknownHostException, IOException { assert from_host != null; assert to_host != null; assert from_db != null; assert to_db != null; assert from_port != null; assert to_port != null; final int MAX_LOOP_SIZE = 15000; // MongoException$CursorNotFound final MongoClient fromClient = new MongoClient(from_host, Integer.parseInt(from_port)); final MongoClient toClient = new MongoClient(to_host, Integer.parseInt(to_port)); final DB fromDb = fromClient.getDB(from_db); final DB toDb = toClient.getDB(to_db); if (!appendCollections) { toDb.dropDatabase();/*from w ww .j av a2 s . c o m*/ } final Set<String> colls = fromDb.getCollectionNames(); for (String cname : colls) { if (cname.equals("system.indexes")) { continue; } final DBCollection fromColl = fromDb.getCollection(cname); final DBCollection toColl = toDb.getCollection(cname); DBCursor cursor = fromColl.find(); int curr = 0; System.out.println("Copying collection: " + cname); while (cursor.hasNext()) { if (curr % MAX_LOOP_SIZE == 0) { if (curr > 0) { cursor.close(); cursor = fromColl.find().skip(curr); if (!cursor.hasNext()) { throw new IOException("hasNext() failed"); } } } final DBObject doc = cursor.next(); final WriteResult ret = toColl.save(doc, WriteConcern.ACKNOWLEDGED); if (!ret.getCachedLastError().ok()) { System.err.println("write error doc id=" + doc.get("_id")); } if (++curr % 1000 == 0) { System.out.println("+++" + curr); } if (displayAllIds) { System.out.println(" id=" + doc.get("_id")); } } cursor.close(); System.out.println(); } }
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// w w w . ja v a 2s . com * @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 w w w. ja v a 2s .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 void fixMissingHttp(final DBCollection coll, final DBCollection hcoll) throws IOException { if (coll == null) { throw new NullPointerException("coll"); }/*from ww w. j a v a 2 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.TabulateField.java
License:Open Source License
private static String getValue(final DBObject obj, final String path) { assert obj != null; assert path != null; final String[] split = path.split(" */ *"); final Matcher mat = Pattern.compile("([^\\[]+)\\[(\\d+)\\]").matcher(""); final int len = split.length; final String str; DBObject dbo = obj; for (int idx = 0; idx < len - 1; idx++) { final String elem = split[idx]; mat.reset(elem);/* ww w . j av a2s .com*/ if (mat.matches()) { //System.out.println("0:" + mat.group(0) + " 1:" + mat.group(1) + " 2:" + mat.group(2)); final BasicDBList lst = (BasicDBList) dbo.get(mat.group(1)); dbo = (DBObject) lst.get(mat.group(2)); if (dbo == null) { break; } } else { dbo = (DBObject) dbo.get(elem); } } if (dbo == null) { str = null; } else { final String elem = split[len - 1]; mat.reset(elem); if (mat.matches()) { final BasicDBList lst = (BasicDBList) dbo.get(mat.group(1)); str = lst.get(mat.group(2)).toString(); } else { str = dbo.get(elem).toString(); } } return str; }