public static void main(String[] args) throws UnknownHostException { MongoClient client = new MongoClient(); DB db = client.getDB("course"); DBCollection collection = db.getCollection("findCriteriaTest"); collection.drop(); for (int i = 0; i < 10; i++) { collection.insert( new BasicDBObject("x", new Random().nextInt(2)).append("y", new Random().nextInt(100))); } QueryBuilder builder = QueryBuilder.start("x").is(0).and("y").greaterThan(10).lessThan(90); // DBObject query = new BasicDBObject("x", 0).append("y", new BasicDBObject("$gt", // 10).append("$lt", 90)); System.out.println("\nCount:"); long count = collection.count(builder.get()); System.out.println(count); System.out.println("\nFind all:"); DBCursor cursor = collection.find(builder.get()); try { while (cursor.hasNext()) { DBObject cur = cursor.next(); System.out.println(cur); } } finally { cursor.close(); } }
@Override public QueryResponse getAllByPositionList(List<Position> positionList, QueryOptions options) { // db.regulatory_region.find({"chunkIds": {$in:["1_200", "1_300"]}, "start": 601156}) String featureType = options.getString("featureType", null); String featureClass = options.getString("featureClass", null); List<DBObject> queries = new ArrayList<>(); for (Position position : positionList) { String chunkId = position.getChromosome() + "_" + getChunkId(position.getPosition(), CHUNKSIZE); BasicDBList chunksId = new BasicDBList(); chunksId.add(chunkId); QueryBuilder builder = QueryBuilder.start("chunkIds").in(chunksId).and("start").is(position.getPosition()); if (featureType != null) { builder.and("featureType").is(featureType); } if (featureClass != null) { builder.and("featureClass").is(featureClass); } // System.out.println("Query: " + builder.get()); queries.add(builder.get()); } System.out.println("Query: " + queries); options = addExcludeReturnFields("chunkIds", options); return executeQueryList(positionList, queries, options); }
public static void main(String[] args) throws UnknownHostException { MongoClient client = new MongoClient(); DB db = client.getDB("course"); DBCollection lines = db.getCollection("dotNotationTest"); lines.drop(); Random rand = new Random(); for (int i = 0; i < 10; i++) { lines.insert( new BasicDBObject("_id", i) .append( "start", new BasicDBObject("x", rand.nextInt(90) + 10).append("y", rand.nextInt(90) + 10)) .append( "end", new BasicDBObject("x", rand.nextInt(90) + 10) .append("y", rand.nextInt(90) + 10))); } QueryBuilder builder = QueryBuilder.start("start.x").greaterThan(50); DBCursor cursor = lines.find(builder.get(), new BasicDBObject("start.y", true).append("_id", false)); try { while (cursor.hasNext()) { DBObject cur = cursor.next(); System.out.println(cur); } } finally { cursor.close(); } }
public void removeOtherBookingsIfAny(String email, String bookingId) { final List<String> extraBookings = new ArrayList<String>(); try { final MongoDatabase mdb = MongoDBConnManager.getInstance().getConnection(); final MongoCollection<Document> coll = mdb.getCollection(DBConstants.COLL_BOOKING); final Document findCr = new Document(); findCr.put(DBConstants.EMAIL, email); final ArrayList<Document> lstBkngs = coll.find(findCr).into(new ArrayList<Document>()); for (final Document document : lstBkngs) { if (!StringUtils.equalsIgnoreCase(bookingId, document.getString(DBConstants.BOOKING_ID))) { extraBookings.add(document.getString(DBConstants.BOOKING_ID)); } } if (!extraBookings.isEmpty()) { QueryBuilder deleteQuery = new QueryBuilder(); deleteQuery .put(DBConstants.BOOKING_ID) .in(extraBookings.toArray(new String[extraBookings.size()])); coll.deleteMany((Bson) deleteQuery.get()); } } catch (Exception e) { e.printStackTrace(); if (e instanceof com.mongodb.MongoTimeoutException) { throw new ApplicationException(MessagesEnum.MONGODB_IS_DOWN.getMessage(), e); } throw new ApplicationException( MessagesEnum.CLOSE_BOOKING_FAILED.getMessage(extraBookings.toString()), e); } }
public static void main(String[] args) throws UnknownHostException { MongoClient client = new MongoClient(); DB db = client.getDB("course"); DBCollection lines = db.getCollection("DotNotationTest"); lines.drop(); Random rand = new Random(); // insert 10 lines with random start and end points for (int i = 0; i < 10; i++) { lines.insert( new BasicDBObject("_id", i) .append( "start", new BasicDBObject("x", rand.nextInt(90) + 10).append("y", rand.nextInt(90) + 10)) .append("end", new BasicDBObject("x", rand.nextInt(90) + 10)) .append("y", rand.nextInt(90) + 10)); } // Filter by x coordinate using . notation QueryBuilder builder = QueryBuilder.start("start.x").greaterThan(50); // Dot notation is also available for field selection DBCursor cursor = lines.find(builder.get(), new BasicDBObject("start.y", true)); try { while (cursor.hasNext()) { DBObject cur = cursor.next(); System.out.println(cur); } } finally { cursor.close(); } }
public List<T> findByAttributes(Map<String, String> attributes) { QueryBuilder qb = QueryBuilder.start(); for (String k : attributes.keySet()) { qb.and("attributes." + k).is(attributes.get(k)); } return makeList(null, getDbCollection().find(qb.get())); }
@Override public List<QueryResult> getAllByIdList(List<String> idList, QueryOptions options) { List<DBObject> queries = new ArrayList<>(); for (String id : idList) { QueryBuilder builder = QueryBuilder.start("name").is(id); // System.out.println("Query: " + builder.get()); queries.add(builder.get()); } // options = addExcludeReturnFields("chunkIds", options); return executeQueryList2(idList, queries, options); }
// DTS public static CtsWorkflowDB getById(String id) { QueryBuilder query = QueryBuilder.start("id").is(id); // Equals List<CtsWorkflowDB> results = new CtsWorkflowDB().getDocumentsByQuery(query.get()); if (results != null && results.size() == 1) { return results.get(0); } return null; }
@Override public List<QueryResult> getAllByRegionList(List<Region> regionList, QueryOptions options) { // db.regulatory_region.find({"chunkIds": {$in:["1_200", "1_300"]}, "start": 601156}) QueryBuilder builder = new QueryBuilder(); List<Object> featureType = options.getList("featureType", null); List<Object> featureClass = options.getList("featureClass", null); // options = addExcludeReturnFields("chunkIds", options); List<DBObject> queries = new ArrayList<>(); for (Region region : regionList) { int firstChunkId = getChunkId(region.getStart(), regulatoryRegionChunkSize); int lastChunkId = getChunkId(region.getEnd(), regulatoryRegionChunkSize); BasicDBList chunksId = new BasicDBList(); for (int j = firstChunkId; j <= lastChunkId; j++) { String chunkId = region.getChromosome() + "_" + j + "_" + regulatoryRegionChunkSize / 1000 + "k"; chunksId.add(chunkId); } // logger.info(chunksId.toString()); builder = builder .start("_chunkIds") .in(chunksId) .and("start") .lessThanEquals(region.getEnd()) .and("end") .greaterThanEquals(region.getStart()); if (featureType != null && featureType.size() > 0) { BasicDBList featureTypeDBList = new BasicDBList(); featureTypeDBList.addAll(featureType); builder = builder.and("featureType").in(featureTypeDBList); } if (featureClass != null && featureClass.size() > 0) { BasicDBList featureClassDBList = new BasicDBList(); featureClassDBList.addAll(featureClass); builder = builder.and("featureClass").in(featureClassDBList); } queries.add(builder.get()); } // System.out.println(">>"+regionList); // System.out.println(">>"+builder.get().toString()); return executeQueryList2(regionList, queries, options); }
protected QueryBuilder appendToBuilder( final QueryBuilder builder, final FilterOp filterOp, final List<Object> rawOperands) { List<String> operands = convertOperandsToString(rawOperands); switch (filterOp) { case EQUALS: if (operands.size() == 1) { builder.is(operands.iterator().next()); } else { builder.in(operands); } break; case NOT_EQUALS: if (operands.size() == 1) { builder.notEquals(operands.iterator().next()); } else { builder.notIn(operands); } break; case LESS: builder.lessThan(operands); break; case LESS_OR_EQUAL: builder.lessThanEquals(operands); break; case GREATER: builder.greaterThan(operands); break; case GREATER_OR_EQUAL: builder.greaterThanEquals(operands); break; default: throw new IllegalArgumentException(filterOp + " no MongoDB equivalent yet"); } return builder; }
public String getLOVId(String LovType, String GlobalVal) throws Exception { String retVal = new ObjectId().toString(); QueryBuilder qm = new QueryBuilder() .and( new QueryBuilder().start().put("LovType").is(LovType).get(), new QueryBuilder().start().put("GlobalVal").is(GlobalVal).get()); BasicDBObject query = new BasicDBObject(); query.putAll(qm.get()); BasicDBList lovItem = (BasicDBList) GetDBList("rwLov", query); if (lovItem != null && lovItem.size() > 0) { BasicDBObject result = (BasicDBObject) lovItem.get(0); retVal = result.get("_id").toString(); } return retVal; }
@Test public void testCountOnCursor() { DBCollection collection = newCollection(); collection.insert(new BasicDBObject("n", 1)); collection.insert(new BasicDBObject("n", 2)); collection.insert(new BasicDBObject("n", 2)); assertEquals(3, collection.find(QueryBuilder.start("n").exists(true).get()).count()); }
public List<DBObject> getSince(String login, Long since) { Date minDate = new Date(since); DBCursor notifications = collection.find( QueryBuilder.start("date").greaterThan(minDate).and("login").notEquals(login).get()); return notifications.toArray(); }
@Override public List<Policy> findByResourceServer( Map<String, String[]> attributes, String resourceServerId, int firstResult, int maxResult) { QueryBuilder queryBuilder = new QueryBuilder().and("resourceServerId").is(resourceServerId); attributes.forEach( (name, value) -> { if ("permission".equals(name)) { if (Boolean.valueOf(value[0])) { queryBuilder.and("type").in(new String[] {"resource", "scope"}); } else { queryBuilder.and("type").notIn(new String[] {"resource", "scope"}); } } else if ("id".equals(name)) { queryBuilder.and("_id").in(value); } else { queryBuilder .and(name) .regex(Pattern.compile(".*" + value[0] + ".*", Pattern.CASE_INSENSITIVE)); } }); DBObject sort = new BasicDBObject("name", 1); return getMongoStore() .loadEntities( PolicyEntity.class, queryBuilder.get(), sort, firstResult, maxResult, invocationContext) .stream() .map(policy -> findById(policy.getId(), resourceServerId)) .collect(toList()); }
@Override public QueryResult next(String chromosome, int position, QueryOptions options) { String featureType = options.getString("featureType", null); String featureClass = options.getString("featureClass", null); BasicDBList chunksId = new BasicDBList(); String chunkId = chromosome + "_" + getChunkId(position, regulatoryRegionChunkSize) + "_" + regulatoryRegionChunkSize / 1000 + "k"; chunksId.add(chunkId); // TODO: Add query to find next item considering next chunk // db.regulatory_region.find({ "chromosome" : "19" , "start" : { "$gt" : 62005} , "featureType" // : "TF_binding_site_motif"}).sort({start:1}).limit(1) QueryBuilder builder; if (options.getString("strand") == null || (options.getString("strand").equals("1") || options.getString("strand").equals("+"))) { // db.core.find({chromosome: "1", start: {$gt: 1000000}}).sort({start: 1}).limit(1) builder = QueryBuilder.start("_chunkIds") .in(chunksId) .and("chromosome") .is(chromosome) .and("start") .greaterThan(position); options.put("sort", new BasicDBObject("start", 1)); options.put("limit", 1); } else { builder = QueryBuilder.start("_chunkIds") .in(chunksId) .and("chromosome") .is(chromosome) .and("end") .lessThan(position); options.put("sort", new BasicDBObject("end", -1)); options.put("limit", 1); } if (featureType != null) { builder.and("featureType").is(featureType); } if (featureClass != null) { builder.and("featureClass").is(featureClass); } System.out.println(builder.get()); return executeQuery("result", builder.get(), options); }
@Override public void addQuery(AdRequest request, QueryBuilder builder) { if (request.day() == null) { return; } int day = request.day().getDay(); if (day == Day.ALL.getDay()) { return; } List<String> days = new ArrayList<String>(); days.add(String.valueOf(day)); days.add(String.valueOf(Day.ALL.getDay())); BasicDBObject dq = new BasicDBObject(AdDBConstants.ADDB_AD_DAY, new BasicDBObject("$in", days)); builder.and(dq); }
protected DBObject transformFieldFilter( final SingleFieldValueFilter<K, T> fieldFilter, final MongoStore<K, T> store) { MongoMapping mapping = store.getMapping(); String dbFieldName = mapping.getDocumentField(fieldFilter.getFieldName()); FilterOp filterOp = fieldFilter.getFilterOp(); List<Object> operands = fieldFilter.getOperands(); QueryBuilder builder = QueryBuilder.start(dbFieldName); builder = appendToBuilder(builder, filterOp, operands); if (!fieldFilter.isFilterIfMissing()) { // If false, the find query will pass if the column is not found. DBObject notExist = QueryBuilder.start(dbFieldName).exists(false).get(); builder = QueryBuilder.start().or(notExist, builder.get()); } return builder.get(); }
@Override public void addQuery(AdRequest request, QueryBuilder builder) { if (request.date() != null) { List<DBObject> condition_queries = new ArrayList<DBObject>(); /* * Prefix der Indizieren beachten. */ for (int i = 0; i < DateConditionDefinition.MAX_PERIOD_COUNT; i++) { QueryBuilder condition = QueryBuilder.start(); // from part of the query BasicDBObject query_from_1 = new BasicDBObject( AdDBConstants.ADDB_AD_DATE_FROM + i, new BasicDBObject("$lte", request.date())); BasicDBObject query_from_2 = new BasicDBObject(AdDBConstants.ADDB_AD_DATE_FROM + i, AdDBConstants.ADDB_AD_DATE_ALL); List<BasicDBObject> queries = new ArrayList<BasicDBObject>(); queries.add(query_from_1); queries.add(query_from_2); BasicDBObject query_from = new BasicDBObject("$or", queries); condition.and(query_from); // to part of the query BasicDBObject query_to_1 = new BasicDBObject( AdDBConstants.ADDB_AD_DATE_TO + i, new BasicDBObject("$gte", request.date())); BasicDBObject query_to_2 = new BasicDBObject(AdDBConstants.ADDB_AD_DATE_TO + i, AdDBConstants.ADDB_AD_DATE_ALL); queries = new ArrayList<BasicDBObject>(); queries.add(query_to_1); queries.add(query_to_2); BasicDBObject query_to = new BasicDBObject("$or", queries); condition.and(query_to); condition_queries.add(condition.get()); } builder.and(new BasicDBObject("$or", condition_queries)); } }
@Override public QueryResponse getAllByRegionList(List<Region> regionList, QueryOptions options) { // db.regulatory_region.find({"chunkIds": {$in:["1_200", "1_300"]}, "start": 601156}) String featureType = options.getString("featureType", null); String featureClass = options.getString("featureClass", null); List<DBObject> queries = new ArrayList<>(); List<String> ids = new ArrayList<>(regionList.size()); for (Region region : regionList) { int firstChunkId = getChunkId(region.getStart(), CHUNKSIZE); int lastChunkId = getChunkId(region.getEnd(), CHUNKSIZE); BasicDBList chunksId = new BasicDBList(); for (int j = firstChunkId; j <= lastChunkId; j++) { String chunkId = region.getChromosome() + "_" + j; chunksId.add(chunkId); } QueryBuilder builder = QueryBuilder.start("chunkIds") .in(chunksId) .and("start") .lessThanEquals(region.getEnd()) .and("end") .greaterThanEquals(region.getStart()); if (featureType != null) { builder.and("featureType").is(featureType); } if (featureClass != null) { builder.and("featureClass").is(featureClass); } System.out.println("Query: " + builder.get()); queries.add(builder.get()); ids.add(region.toString()); } options = addExcludeReturnFields("chunkIds", options); return executeQueryList(ids, queries, options); }
public List<T> findByTags(EntityFilter<T> filter, String... tags) { return makeList(filter, getDbCollection().find(QueryBuilder.start("tags").all(tags).get())); }
public List<T> findByNamePattern(String name) { return makeList( null, getDbCollection().find(QueryBuilder.start("name").regex(Pattern.compile(name)).get())); }
/** @see rexos.HAL.libraries.blackboard_client.BlackboardSubscription#getQuery() */ @Override public DBObject getQuery() { return QueryBuilder.start(OplogEntry.OPERATION_FIELD).is(operation.getOpCode()).get(); }