public List<DBObject> getSourceBrowsers() { DBObject groupFields = new BasicDBObject("_id", "$device"); groupFields.put("sum", new BasicDBObject("$sum", "$sum")); DBObject group = new BasicDBObject("$group", groupFields); DBObject sort = new BasicDBObject("$sort", new BasicDBObject("sum", 1)); List<DBObject> pipeline = Arrays.asList(group, sort); // allowDiskUse AggregationOptions options = AggregationOptions.builder().allowDiskUse(true).batchSize(10000).build(); Cursor cursor = device.aggregate(pipeline, options); List<DBObject> list = new ArrayList<DBObject>(); while (cursor.hasNext()) { DBObject object = cursor.next(); DBObject newObj = new BasicDBObject(); newObj.put("device", object.get("_id")); newObj.put("sum", object.get("sum")); list.add(newObj); } cursor.close(); return list; }
/** * Adds privilege documents to the {@code system.users} collection in a database, which creates * database credentials in MongoDB. * * @param username * @param passwd * @param readOnly if true, user will only be able to read * @throws MongoException */ public WriteResult addUser(String username, char[] passwd, boolean readOnly) { DBCollection c = getCollection("system.users"); DBObject o = c.findOne(new BasicDBObject("user", username)); if (o == null) o = new BasicDBObject("user", username); o.put("pwd", _hash(username, passwd)); o.put("readOnly", readOnly); return c.save(o); }
@Override public void write(String hash, String way) throws InterruptedException { DBObject dbObject = new BasicDBObject(); dbObject.put("hash", hash); if (!isContain(dbObject)) { dbObject.put("way", way); hashColl.insert(dbObject); } Thread.sleep(300); }
@Test public void saveThisTweet() throws Exception { Tweet tweet = new Tweet("100000", "XebiaFR", new Date(), "Discovering #mongodb"); DBObject dbObject = new BasicDBObject(); dbObject.put("from_user_id", tweet.getUserId()); dbObject.put("from_user_name", tweet.getUserName()); dbObject.put("created_at", tweet.getCreateAt()); dbObject.put("text", tweet.getText()); collection.save(dbObject, WriteConcern.SAFE); assertThat(collection.count()).isGreaterThan(0); }
private void handleUser(ISFSEvent event) { trace("Registered user logged in", userName); DBCollection users = AuthorizeExtension.users; BasicDBObject query = new BasicDBObject(); query.put("email", userName); DBCursor cursor = users.find(query); if (!cursor.hasNext()) { trace("user not found"); return; } document = cursor.next(); String password_digest = (String) document.get("password_digest"); if (!getApi().checkSecurePassword(session, password_digest, cryptedPass)) { trace("password wrong"); return; } document.put("session", generateSession); users.update(query, document); isRegistered = true; }
/** * Forces the master server to fsync the RAM data to disk, then lock all writes. The database will * be read-only after this command returns. * * @return result of the command execution * @throws MongoException * @mongodb.driver.manual reference/command/fsync/ fsync command */ public CommandResult fsyncAndLock() { DBObject cmd = new BasicDBObject("fsync", 1); cmd.put("lock", 1); CommandResult result = getDB(ADMIN_DATABASE_NAME).command(cmd); result.throwOnError(); return result; }
@Override public void apply(DBObject query) { final BasicDBObject object = new BasicDBObject(); object.put("$gte", getDBValue(start)); object.put("$lt", getDBValue(end)); query.put(getField(), object); }
/** * Forces the master server to fsync the RAM data to disk This is done automatically by the server * at intervals, but can be forced for better reliability. * * @param async if true, the fsync will be done asynchronously on the server. * @return result of the command execution * @throws MongoException * @mongodb.driver.manual reference/command/fsync/ fsync command */ public CommandResult fsync(boolean async) { DBObject cmd = new BasicDBObject("fsync", 1); if (async) { cmd.put("async", 1); } CommandResult result = getDB(ADMIN_DATABASE_NAME).command(cmd); result.throwOnError(); return result; }
public void updateValidity(String user, String token) { DBCollection coll = db().getCollection(M_TOKENS); BasicDBObject query = new BasicDBObject(); query.put("user", user); query.put("token", token); DBCursor cursor = coll.find(query); while (cursor.hasNext()) { DBObject doc = cursor.next(); doc.put("validity", System.currentTimeMillis()); coll.save(doc, WriteConcern.SAFE); } }
private DBObject getTagSet(String tagSetString) { DBObject tagSet = new BasicDBObject(); if (tagSetString.length() > 0) { for (String tag : tagSetString.split(",")) { String[] tagKeyValuePair = tag.split(":"); if (tagKeyValuePair.length != 2) { throw new IllegalArgumentException("Bad read preference tags: " + tagSetString); } tagSet.put(tagKeyValuePair[0].trim(), tagKeyValuePair[1].trim()); } } return tagSet; }
private void _check() throws MongoException { if (_it != null) return; if (_collection != null && _query != null) { _lookForHints(); DBObject foo = _query; if (hasSpecialQueryFields()) { foo = _specialFields == null ? new BasicDBObject() : _specialFields; _addToQueryObject(foo, "query", _query, true); _addToQueryObject(foo, "orderby", _orderBy, false); if (_hint != null) _addToQueryObject(foo, "$hint", _hint); if (_hintDBObj != null) _addToQueryObject(foo, "$hint", _hintDBObj); if (_explain) foo.put("$explain", true); if (_snapshot) foo.put("$snapshot", true); } _it = _collection.__find( foo, _keysWanted, _skip, _batchSize, _limit, _options, _readPref, _decoderFact.create()); } if (_it == null) { _it = (new LinkedList<DBObject>()).iterator(); _fake = true; } }
// 获得一天的转换漏斗 public List<DBObject> getConversionFunnel(String date) { SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); DBObject match = null; try { match = new BasicDBObject("$match", new BasicDBObject("date", sdf.parse(date))); } catch (ParseException e) { } DBObject groupFields = new BasicDBObject("_id", ""); groupFields.put("step1sum", new BasicDBObject("$sum", "$step1")); groupFields.put("step2sum", new BasicDBObject("$sum", "$step2")); groupFields.put("step3sum", new BasicDBObject("$sum", "$step3")); DBObject group = new BasicDBObject("$group", groupFields); // DBObject sort = new BasicDBObject("$sort", new BasicDBObject("sum", 1)); List<DBObject> pipeline = Arrays.asList(match, group); // allowDiskUse AggregationOptions options = AggregationOptions.builder().allowDiskUse(true).batchSize(10000).build(); Cursor cursor = CF.aggregate(pipeline, options); List<DBObject> list = new ArrayList<DBObject>(); while (cursor.hasNext()) { DBObject object = cursor.next(); DBObject newObj = new BasicDBObject(); // DBObject _id = (DBObject)object.get("_id"); // newObj.put("date",sdf.format(_id.get("$date"))); DBObject newObj1 = new BasicDBObject(); newObj1.put("step", "访问本站"); newObj1.put("num", object.get("step1sum")); list.add(newObj1); DBObject newObj2 = new BasicDBObject(); newObj2.put("step", "浏览商品"); newObj2.put("num", object.get("step2sum")); list.add(newObj2); DBObject newObj3 = new BasicDBObject(); newObj3.put("step", "完成交易"); newObj3.put("num", object.get("step3sum")); list.add(newObj3); } cursor.close(); return list; }
private BasicDBList processGeneList(String genes) { BasicDBList list = new BasicDBList(); Client wsRestClient = Client.create(); WebResource webResource = wsRestClient.resource("http://ws.bioinfo.cipf.es/cellbase/rest/latest/hsa/feature/gene/"); ObjectMapper mapper = new ObjectMapper(); String response = webResource.path(genes).path("info").queryParam("of", "json").get(String.class); try { JsonNode actualObj = mapper.readTree(response); Iterator<JsonNode> it = actualObj.iterator(); Iterator<JsonNode> aux; while (it.hasNext()) { JsonNode node = it.next(); if (node.isArray()) { aux = node.iterator(); while (aux.hasNext()) { JsonNode auxNode = aux.next(); DBObject regionClause = new BasicDBObject("chr", auxNode.get("chromosome").asText()); regionClause.put( "pos", new BasicDBObject("$gte", auxNode.get("start").asInt()) .append("$lte", auxNode.get("end").asInt())); list.add(regionClause); } } } } catch (IOException e) { e.printStackTrace(); } return list; }
private DBObject getQuerySort(String sort) { DBObject res = new BasicDBObject(); // sort=[{"property":"stats_id_snp","direction":"ASC"}], // Pattern pattern = Pattern.compile("(\\w+):(\\d+)-(\\d+)"); Pattern pattern = Pattern.compile("\"property\":\"(\\w+)\",\"direction\":\"(ASC|DESC)\""); Matcher matcher = pattern.matcher(sort); if (matcher.find()) { String field = matcher.group(1); String direction = matcher.group(2); int dir = 1; if (direction.equalsIgnoreCase("ASC")) { dir = 1; } else if (direction.equalsIgnoreCase("DESC")) { dir = -1; } switch (field) { case "chromosome": res.put("chr", dir); res.put("pos", dir); break; case "snpid": res.put("sources.snpId", dir); break; case "consecuente_types": res.put("sources.effects", dir); break; case "genes": res.put("sources.genes.1", dir); break; case "polyphen_score": res.put("sources.attributes.PolyphenScore", dir); break; case "sift_score": res.put("sources.attributes.SIFTScore", dir); break; } } return res; }
void _addToQueryObject(DBObject query, String field, Object thing) { if (thing == null) return; query.put(field, thing); }
public QueryResult<VariantInfo> getRecordsMongo( int page, int start, int limit, MutableInt count, Map<String, String> options) { long startTime = System.currentTimeMillis(); QueryResult<VariantInfo> queryResult = new QueryResult<>(); List<VariantInfo> res = new ArrayList<>(); String sourceId = options.get("studyId"); DBCollection coll = db.getCollection("variants"); BasicDBObject elemMatch = new BasicDBObject("sourceId", sourceId); DBObject query = new BasicDBObject(); BasicDBList orList = new BasicDBList(); Map<String, List<String>> sampleGenotypes = processSamplesGT(options); System.out.println("map = " + options); if (options.containsKey("region") && !options.get("region").equals("")) { String[] regions = options.get("region").split(","); Pattern pattern = Pattern.compile("(\\w+):(\\d+)-(\\d+)"); Matcher matcher, matcherChr; for (int i = 0; i < regions.length; i++) { String region = regions[i]; matcher = pattern.matcher(region); if (matcher.find()) { String chr = matcher.group(1); int s = Integer.valueOf(matcher.group(2)); int e = Integer.valueOf(matcher.group(3)); DBObject regionClause = new BasicDBObject("chr", chr); regionClause.put("pos", new BasicDBObject("$gte", s).append("$lte", e)); orList.add(regionClause); } else { Pattern patternChr = Pattern.compile("(\\w+)"); matcherChr = patternChr.matcher(region); if (matcherChr.find()) { String chr = matcherChr.group(); DBObject regionClause = new BasicDBObject("chr", chr); orList.add(regionClause); } } } query.put("$or", orList); } else if (options.containsKey("genes") && !options.get("genes").equals("")) { orList = processGeneList(options.get("genes")); if (orList.size() > 0) { query.put("$or", orList); } else { queryResult.setWarningMsg("Wrong gene name"); queryResult.setResult(res); queryResult.setNumResults(res.size()); return queryResult; } } if (options.containsKey("conseq_type") && !options.get("conseq_type").equals("")) { String[] cts = options.get("conseq_type").split(","); BasicDBList ctList = new BasicDBList(); for (String ct : cts) { ctList.add(ct); } elemMatch.put("effects", new BasicDBObject("$in", ctList)); } if (sampleGenotypes.size() > 0) { for (Map.Entry<String, List<String>> entry : sampleGenotypes.entrySet()) { BasicDBList gtList = new BasicDBList(); for (String gt : entry.getValue()) { gtList.add(gt); } elemMatch.put("samples." + entry.getKey() + ".GT", new BasicDBObject("$in", gtList)); } } if (options.containsKey("miss_gt") && !options.get("miss_gt").equalsIgnoreCase("")) { Integer val = Integer.valueOf(options.get("miss_gt")); Object missGt = getMongoOption(options.get("option_miss_gt"), val); elemMatch.put("stats.missGenotypes", missGt); } BasicDBList andControls = new BasicDBList(); if (options.containsKey("maf_1000g_controls") && !options.get("maf_1000g_controls").equalsIgnoreCase("")) { BasicDBList or = new BasicDBList(); or.add(new BasicDBObject("attributes.1000G_maf", new BasicDBObject("$exists", false))); or.add( new BasicDBObject( "attributes.1000G_maf", new BasicDBObject("$lte", options.get("maf_1000g_controls")))); andControls.add(new BasicDBObject("$or", or)); } if (options.containsKey("maf_1000g_afr_controls") && !options.get("maf_1000g_afr_controls").equalsIgnoreCase("")) { BasicDBList or = new BasicDBList(); or.add(new BasicDBObject("attributes.1000G_AFR_maf", new BasicDBObject("$exists", false))); or.add( new BasicDBObject( "attributes.1000G_AFR_maf", new BasicDBObject("$lte", options.get("maf_1000g_afr_controls")))); andControls.add(new BasicDBObject("$or", or)); } if (options.containsKey("maf_1000g_asi_controls") && !options.get("maf_1000g_asi_controls").equalsIgnoreCase("")) { BasicDBList or = new BasicDBList(); or.add(new BasicDBObject("attributes.1000G_ASI_maf", new BasicDBObject("$exists", false))); or.add( new BasicDBObject( "attributes.1000G_ASI_maf", new BasicDBObject("$lte", options.get("maf_1000g_asi_controls")))); andControls.add(new BasicDBObject("$or", or)); } if (options.containsKey("maf_1000g_eur_controls") && !options.get("maf_1000g_eur_controls").equalsIgnoreCase("")) { System.out.print("EUR"); BasicDBList or = new BasicDBList(); or.add(new BasicDBObject("attributes.1000G_EUR_maf", new BasicDBObject("$exists", false))); or.add( new BasicDBObject( "attributes.1000G_EUR_maf", new BasicDBObject("$lte", options.get("maf_1000g_eur_controls")))); andControls.add(new BasicDBObject("$or", or)); } if (options.containsKey("maf_1000g_ame_controls") && !options.get("maf_1000g_ame_controls").equalsIgnoreCase("")) { System.out.print("AME"); BasicDBList or = new BasicDBList(); or.add(new BasicDBObject("attributes.1000G_AME_maf", new BasicDBObject("$exists", false))); or.add( new BasicDBObject( "attributes.1000G_AME_maf", new BasicDBObject("$lte", options.get("maf_1000g_ame_controls")))); andControls.add(new BasicDBObject("$or", or)); } if (options.containsKey("maf_evs_controls") && !options.get("maf_evs_controls").equalsIgnoreCase("")) { BasicDBList or = new BasicDBList(); or.add(new BasicDBObject("attributes.EVS_maf", new BasicDBObject("$exists", false))); or.add( new BasicDBObject( "attributes.EVS_maf", new BasicDBObject("$lte", options.get("maf_evs_controls")))); andControls.add(new BasicDBObject("$or", or)); } if (options.containsKey("maf_bier_controls") && !options.get("maf_bier_controls").equalsIgnoreCase("")) { BasicDBList or = new BasicDBList(); or.add(new BasicDBObject("attributes.BIER_maf", new BasicDBObject("$exists", false))); or.add( new BasicDBObject( "attributes.BIER_maf", new BasicDBObject("$lte", options.get("maf_bier_controls")))); andControls.add(new BasicDBObject("$or", or)); } if (andControls.size() > 0) { elemMatch.append("$and", andControls); } query.put("sources", new BasicDBObject("$elemMatch", elemMatch)); System.out.println("#############################"); System.out.println(query); System.out.println("#############################"); long dbStart = System.currentTimeMillis(); DBObject sort = null; DBCursor cursor; if (options.containsKey("sort")) { sort = getQuerySort(options.get("sort")); cursor = coll.find(query).sort(sort).skip(start).limit(limit); } else { cursor = coll.find(query).skip(start).limit(limit); } count.setValue(cursor.count()); queryResult.setDbTime(dbStart - System.currentTimeMillis()); for (DBObject obj : cursor) { BasicDBObject elem = (BasicDBObject) obj; VariantInfo vi = new VariantInfo(); VariantStats vs = new VariantStats(); String chr = elem.getString("chr"); int pos = elem.getInt("pos"); vi.setChromosome(chr); vi.setPosition(pos); BasicDBList studies = (BasicDBList) elem.get("sources"); Iterator<Object> it = studies.iterator(); while (it.hasNext()) { BasicDBObject study = (BasicDBObject) it.next(); if (study.getString("sourceId").equalsIgnoreCase(sourceId)) { BasicDBObject stats = (BasicDBObject) study.get("stats"); String ref = study.getString("ref"); BasicDBList alt = (BasicDBList) study.get("alt"); vi.setRef(ref); vi.setAlt(Joiner.on(",").join(alt.toArray())); vs.setMaf((float) stats.getDouble("maf")); vs.setMgf((float) stats.getDouble("mgf")); vs.setMafAllele(stats.getString("alleleMaf")); vs.setMgfAllele(stats.getString("genotypeMaf")); vs.setMissingAlleles(stats.getInt("missAllele")); vs.setMissingGenotypes(stats.getInt("missGenotypes")); vs.setMendelinanErrors(stats.getInt("mendelErr")); vs.setCasesPercentDominant((float) stats.getDouble("casesPercentDominant")); vs.setControlsPercentDominant((float) stats.getDouble("controlsPercentDominant")); vs.setCasesPercentRecessive((float) stats.getDouble("casesPercentRecessive")); vs.setControlsPercentRecessive((float) stats.getDouble("controlsPercentRecessive")); BasicDBObject samples = (BasicDBObject) study.get("samples"); for (String sampleName : samples.keySet()) { DBObject sample = (DBObject) samples.get(sampleName); if (sample.containsField("GT")) { String sampleGT = (String) sample.get("GT"); vi.addSammpleGenotype(sampleName, sampleGT); } } vi.setSnpid((String) study.get("snpId")); if (study.containsField("effects")) { BasicDBList conseqTypes = (BasicDBList) study.get("effects"); conseqTypes.remove(""); String cts = Joiner.on(",").join(conseqTypes.iterator()); vi.addConsequenceTypes(cts); } if (study.containsField("genes")) { BasicDBList genesList = (BasicDBList) study.get("genes"); String genes = Joiner.on(",").join(genesList.iterator()); vi.addGenes(genes); } if (study.containsField("attributes")) { BasicDBObject attr = (BasicDBObject) study.get("attributes"); if (attr.containsField("1000G_maf")) { vi.addControl("1000G_maf", (String) attr.get("1000G_maf")); vi.addControl("1000G_amaf", (String) attr.get("1000G_amaf")); vi.addControl("1000G_gt", (String) attr.get("1000G_gt")); } if (attr.containsField("1000G_ASI_maf")) { vi.addControl("1000G-ASI_maf", (String) attr.get("1000G_ASI_maf")); vi.addControl("1000G-ASI_amaf", (String) attr.get("1000G_ASI_amaf")); vi.addControl("1000G-ASI_gt", (String) attr.get("1000G_ASI_gt")); } if (attr.containsField("1000G_AFR_maf")) { vi.addControl("1000G-AFR_maf", (String) attr.get("1000G_AFR_maf")); vi.addControl("1000G-AFR_amaf", (String) attr.get("1000G_AFR_amaf")); vi.addControl("1000G-AFR_gt", (String) attr.get("1000G_AFR_gt")); } if (attr.containsField("1000G_AME_maf")) { vi.addControl("1000G-AME_maf", (String) attr.get("1000G_AME_maf")); vi.addControl("1000G-AME_amaf", (String) attr.get("1000G_AME_amaf")); vi.addControl("1000G-AME_gt", (String) attr.get("1000G_AME_gt")); } if (attr.containsField("1000G_EUR_maf")) { vi.addControl("1000G-EUR_maf", (String) attr.get("1000G_EUR_maf")); vi.addControl("1000G-EUR_amaf", (String) attr.get("1000G_EUR_amaf")); vi.addControl("1000G-EUR_gt", (String) attr.get("1000G_EUR_gt")); } if (attr.containsField("EVS_maf")) { vi.addControl("EVS_maf", (String) attr.get("EVS_maf")); vi.addControl("EVS_amaf", (String) attr.get("EVS_amaf")); vi.addControl("EVS_gt", (String) attr.get("EVS_gt")); } if (attr.containsField("BIER_maf")) { vi.addControl("BIER_maf", (String) attr.get("BIER_maf")); vi.addControl("BIER_amaf", (String) attr.get("BIER_amaf")); vi.addControl("BIER_gt", (String) attr.get("BIER_gt")); } if (attr.containsField("PolyphenScore")) { vi.setPolyphen_score(Double.parseDouble(attr.getString("PolyphenScore"))); vi.setPolyphen_effect(Integer.parseInt(attr.getString("PolyphenEffect"))); } if (attr.containsField("SIFTScore")) { vi.setSift_score(Double.parseDouble(attr.getString("SIFTScore"))); vi.setSift_effect(Integer.parseInt(attr.getString("SIFTEffect"))); } } continue; } } vi.addStats(vs); res.add(vi); } queryResult.setResult(res); queryResult.setTime(startTime - System.currentTimeMillis()); return queryResult; }
public static void main(String[] args) { try { // connect to mongoDB, ip and port number Mongo mongo = new Mongo("127.0.0.1", 27017); // get database from MongoDB, // if database doesn't exists, mongoDB will create it automatically DB db = mongo.getDB("yourdb"); // Getting A List Of Collections Set<String> collections = db.getCollectionNames(); for (String s : collections) { System.out.println(s); } // Get collection from MongoDB, database named "yourDB" // if collection doesn't exists, mongoDB will create it automatically DBCollection collection = db.getCollection("yourCollection"); // create a document to store key and value DBObject document = new BasicDBObject(); document.put("id", 1001); document.put("msg", "hello world mongoDB in Java"); // save it into collection named "yourCollection" collection.insert(document); // search query DBObject searchQuery = new BasicDBObject(); searchQuery.put("id", 1001); // query it DBCursor cursor = collection.find(searchQuery); // loop over the cursor and display the retrieved result while (cursor.hasNext()) { System.out.println("Our collection after putting document here: " + cursor.next()); } // Counting Documents in Collection System.out.println("Elements in collection " + collection.getCount()); // update document (just replase exist - it is normal practise) DBObject updatedDocument = new BasicDBObject(); updatedDocument.put("id", 1001); updatedDocument.put("msg", "hello world mongoDB in Java updated"); collection.update(new BasicDBObject().append("id", 1001), updatedDocument); // query it DBCursor cursorAfterUpdate = collection.find(searchQuery); // loop over the cursor and display the retrieved result while (cursorAfterUpdate.hasNext()) { System.out.println("Our collection after update: " + cursorAfterUpdate.next()); } // Counting Documents in Collection System.out.println("Elements in collection " + collection.getCount()); // Map to object Message message = new Message(); message.setId((Integer) document.get("id")); message.setMessage((String) document.get("msg")); System.out.println("Id putted in object: " + message.getId()); System.out.println("Message putted in object: " + message.getMessage()); // Remove document from collection DBObject doc = collection.findOne(); // get first document collection.remove(doc); // query it DBCursor cursorAfterDelete = collection.find(searchQuery); // loop over the cursor and display the retrieved result while (cursorAfterDelete.hasNext()) { System.out.println("Our collection after delete: " + cursorAfterDelete.next()); } // Counting Documents in Collection System.out.println("Elements in collection " + collection.getCount()); // Close connection to db mongo.close(); System.out.println("Done"); } catch (UnknownHostException e) { e.printStackTrace(); } catch (MongoException e) { e.printStackTrace(); } }
private DBObject hashObject() { DBObject obj = new BasicDBObject(); obj.put("hash", ""); obj.put("way", ""); return obj; }
private DBObject convertToDBObject(Object pojo, Object id) { BsonDocument document = marshallDocument(pojo); DBObject dbo = new AlreadyCheckedDBObject(document.toByteArray(), id); dbo.put("_id", id); return dbo; }
public CreateResponse create(ShowToSave objectToSave) { DBObject newObject = mapper.map(objectToSave, BasicDBObject.class); newObject.put("alias", objectToSave.getAlias()); db.getCollection("show").insert(newObject); return new CreateResponse(((ObjectId) newObject.get("_id")).toHexString()); }