List of usage examples for com.mongodb BasicDBObject BasicDBObject
public BasicDBObject(final String key, final Object value)
From source file:bhl.pages.database.MongoConnection.java
License:Open Source License
/** * Fetch a resource from the server via a given field value * @param collName the collection or database name * @param value the value of the field/* w w w . j a va2 s .c o m*/ * @param field the field name * @return the response as a string or null if not found */ @Override public String getFromDbByField(String collName, String value, String field) throws DbException { try { connect(); DBCollection coll = getCollectionFromName(collName); DBObject query; if (field.equals(JSONKeys._ID)) { ObjectId objId = new ObjectId(value); query = new BasicDBObject(field, objId); } else query = new BasicDBObject(field, value); DBObject obj = coll.findOne(query); if (obj != null) return obj.toString(); else return null; } catch (Exception e) { throw new DbException(e); } }
From source file:biopolis.headless.BiopolisPersistencyLayer.java
public BiopolisPersistencyLayer(BiopolisProperties props) throws BiopolisGeneralException, SQLException { org.neo4j.jdbc.Driver driver = new org.neo4j.jdbc.Driver(); this.connNeo4j = driver.connect("jdbc:neo4j://" + props.neo4j_endpoint, new Properties()); try {//from www . j a v a 2 s.co m System.out.println(props.mongo_host); MongoClientOptions mco = new MongoClientOptions.Builder().connectionsPerHost(10) .threadsAllowedToBlockForConnectionMultiplier(10).build(); mongo = new MongoClient(new ServerAddress("localhost"), mco); //addresses is a pre-populated List } catch (MongoException e) { e.printStackTrace(); throw new BiopolisGeneralException("Mongo exception problem"); } catch (UnknownHostException e) { e.printStackTrace(); throw new BiopolisGeneralException("Mongo connection problem"); } DB dbo = mongo.getDB(BiopolisPersistencyLayer.dbName); if (dbo.collectionExists(BiopolisPersistencyLayer.collectionNamePlaces)) { this.dbcolPlaces = dbo.getCollection(BiopolisPersistencyLayer.collectionNamePlaces); } else { this.dbcolPlaces = dbo.createCollection(BiopolisPersistencyLayer.collectionNamePlaces, null); this.dbcolPlaces.createIndex(new BasicDBObject("loc", "2dsphere")); } if (dbo.collectionExists(collectionNameSearches)) { this.dbcolSearches = dbo.getCollection(collectionNameSearches); } else { this.dbcolSearches = dbo.createCollection(collectionNameSearches, null); this.dbcolSearches.createIndex(new BasicDBObject("biopolisttl", 1), new BasicDBObject("expireAfterSeconds", props.expire_search)); } this.segSZ = props.segment_size; }
From source file:biopolis.headless.BiopolisSegmentationManagement.java
public java.util.Date updateTTL() throws BiopolisGeneralException { java.util.Date date = new java.util.Date(); DBCursor cur = dbcol.find(new BasicDBObject("_id", id)); if (!cur.hasNext()) { throw new BiopolisGeneralException("Search trashed"); }//from w w w .jav a 2s.c o m BasicDBObject setter = new BasicDBObject("$set", new BasicDBObject("biopolisttl", date)); dbcol.update(new BasicDBObject("_id", id), setter); dbcol.update(new BasicDBObject("biopolishandle", this.biopiolisid), setter); return date; }
From source file:biopolis.headless.BiopolisSegmentationManagement.java
public BiopolisSegmentation finalPhase(String nodetype) throws SQLException, BiopolisGeneralException { java.util.Date date = this.updateTTL(); List ids = dbcol.distinct("biopolisdata", new BasicDBObject("biopolishandle", this.biopiolisid)); for (Object id : ids) { BasicDBObject aggregate = new BasicDBObject(); aggregate.put("biopolishandle", this.biopiolisid); aggregate.put("biopolisdata", (Long) id); long count = dbcol.count(aggregate); if (count == this.occurs) { BasicDBObject insertspec = new BasicDBObject(); insertspec.put("biopolisid", this.biopiolisid); insertspec.put("biopolisdata", (Long) id); insertspec.put("biopolisttl", date); dbcol.insert(insertspec);//www. j a va2 s . com } } long count = dbcol.count(new BasicDBObject("biopolisid", this.biopiolisid)); //finalize segmentation BasicDBObject place = new BasicDBObject(); place.put("biopolisnodetype", nodetype); place.put("biopoliscount", count); place.put("biopolisttl", new java.util.Date()); BasicDBObject setter = new BasicDBObject("$set", place); dbcol.update(new BasicDBObject("_id", id), setter); BiopolisSegmentation segmentation = new BiopolisSegmentation(); segmentation.nodetype = nodetype; segmentation.results = count; segmentation.biopolisid = this.biopiolisid; date = new java.util.Date(); BasicDBObject updater = new BasicDBObject("$set", new BasicDBObject("biopolisttl", date)); dbcol.update(new BasicDBObject("_id", this.id), updater); dbcol.update(new BasicDBObject("biopolisid", this.biopiolisid), updater); return segmentation; }
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); }/*from w ww. ja v a 2 s . co 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:bom.BOM.java
/** * @param args the command line arguments *//*from www.java2 s . com*/ public static void main(String[] args) { // TODO code application logic here try { MongoClient mongoClient = new MongoClient("localhost", 5000); // Now connect to your databases //mongod --port 5000 --dbpath D:\mongodbdata\ (please mention above created path) //mongo --port 5000 DB db = mongoClient.getDB("bomdb"); System.out.println("Connect to database successfully"); //DBCollection col1 = db.createCollection("Parts",p1); DBCollection col2 = db.getCollection("BOM"); System.out.println("Collection created successfully"); List<BillOfMaterial> list; LocalDataSource ds = new LocalDataSource(); list = ds.LoadBom(1000); // To connect to mongodb server for (BillOfMaterial Element : list) { for (Parts ElementPart : Element.partList) { for (Operation ElementOp : ElementPart.optList) { BasicDBObject doc = new BasicDBObject("BOM_Name", Element.getBOM_Name()) .append("Level", Element.getLevel()).append("Stock", Element.getStock()). append("Parts", new BasicDBObject("Part_ID", ElementPart.getPart_ID()) .append("Part_Name", ElementPart.getPart_Name()) .append("Diameter", ElementPart.getDiamter()) .append("Dept", ElementPart.getDept()).append("Area", ElementPart.getArea()) .append("Quantity", ElementPart.getQuantity()). append("Material", new BasicDBObject("Material_ID", ElementPart.getMaterial_ID()) .append("Material_Name", ElementPart.getMaterial_Name()). append("Operation", new BasicDBObject("Operation_ID", ElementOp.getOperation_ID()) .append("Operation_Name", ElementOp.getOperation_Name()) .append("Equipment_Name", ElementOp.getEquipment_Name()) .append("No_of_Staff", ElementOp.getNo_of_Staff()) .append("Operation_Time", ElementOp .getOperation_Time() .get(Calendar.MINUTE) * ElementOp.getOperation_Time() .get(Calendar.SECOND)) .append("Assembly_Time", ElementOp .getAssembly_Time() .get(Calendar.MINUTE) * ElementOp.getAssembly_Time() .get(Calendar.SECOND)) .append("Set_Up_Time", ElementOp .getSet_Up_Time() .get(Calendar.MINUTE) * ElementOp.getSet_Up_Time() .get(Calendar.SECOND)) . append("Contractor", new BasicDBObject( "Contractor_ID", ElementOp.getContractor_ID()) .append("Contractor_Name", ElementOp .getContractor_Name()))))); col2.insert(doc); } } } System.out.println("Document inserted successfully"); } catch (Exception e) { System.err.println(e.getClass().getName() + ": " + e.getMessage()); } }
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"); }/*from w w w.ja va 2 s .co 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.BrokenLinks.java
License:Open Source License
private static int getIsisIdField(final String mstName, final DBCollection coll) throws IOException { assert mstName != null; assert coll != null; final BasicDBObject query = new BasicDBObject(MST_FIELD, mstName); final BasicDBObject doc = (BasicDBObject) coll.findOne(query); if (doc == null) { throw new IOException( "Missing fields: collection[" + coll.getName() + "] or master name[" + mstName + "]"); }//from www .ja v a 2s.c o m return doc.getInt(ID_TAG_FIELD); }
From source file:br.bireme.scl.BrokenLinks.java
License:Open Source License
private static int getIsisUrlFields(final String mstName, final DBCollection coll) throws IOException { assert mstName != null; assert coll != null; final BasicDBObject query = new BasicDBObject(MST_FIELD, mstName); final BasicDBObject doc = (BasicDBObject) coll.findOne(query); if (doc == null) { throw new IOException( "Missing fields: collection[" + coll.getName() + "] or master name[" + mstName + "]"); }//from www. j av a 2 s . c o m return doc.getInt(URL_TAG_FIELD); }
From source file:br.bireme.scl.BrokenLinks.java
License:Open Source License
private static boolean saveRecord(final String mstName, final int id, final String url, final String err, final int urlTag, final List<Integer> ccsFlds, final Master mst, final DBCollection coll, final DBCollection hcoll, final Map<Integer, Map<String, Integer>> occMap) throws BrumaException, IOException { assert mstName != null; assert id > 0; assert url != null; assert urlTag > 0; assert err != null; assert ccsFlds != null; assert mst != null; assert coll != null; assert hcoll != null; assert occMap != null; final Record rec = mst.getRecord(id); if (!rec.isActive()) { //throw new BrumaException("not active record mfn=" + id); System.err.println("WARNING: record[" + id + "] is not active. " + "Ignoring it!"); return false; }//ww w . j a va2 s . c o m final List<Field> urls = rec.getFieldList(urlTag); final Date now = new Date(); Date date; Map<String, Integer> fldMap = occMap.get(id); if (fldMap == null) { fldMap = new HashMap<String, Integer>(); occMap.put(id, fldMap); } final int occ = nextOcc(url, urls, fldMap); if (occ == -1) { System.err.println("url[" + url + "] not found. mfn=" + id); //throw new IOException("url[" + url + "] not found. mfn=" + id); return false; } final BasicDBObject query = new BasicDBObject(ID_FIELD, id + "_" + occ); final boolean ret; if (fixedRecently(hcoll, query, now, 60)) { ret = false; } else { final BasicDBObject obj = (BasicDBObject) coll.findOne(query); if (obj == null) { date = now; } else { date = obj.getDate(LAST_UPDATE_FIELD); if (date == null) { date = obj.getDate(DATE_FIELD); } else { final WriteResult wr = coll.remove(obj, WriteConcern.ACKNOWLEDGED); if (!wr.getCachedLastError().ok()) { //TODO } date = obj.getDate(DATE_FIELD); } } final String url_d = EncDecUrl.decodeUrl(url); final String url_d_l = (url_d.length() >= 900) ? url_d.substring(0, 900) + "..." : url_d; final String url_l = (url.length() > 900) ? url.substring(0, 900) + "..." : url; final BasicDBObject doc = new BasicDBObject(); doc.put(DATE_FIELD, date); doc.put(LAST_UPDATE_FIELD, now); doc.put(MST_FIELD, mstName); doc.put(ID_FIELD, id + "_" + occ); doc.put(BROKEN_URL_FIELD, url_l); doc.put(PRETTY_BROKEN_URL_FIELD, url_d_l); doc.put(MSG_FIELD, err); doc.put(CENTER_FIELD, getCCS(rec, ccsFlds)); final WriteResult wres = coll.save(doc, WriteConcern.ACKNOWLEDGED); ret = wres.getCachedLastError().ok(); } return ret; }