List of usage examples for com.mongodb.client.model Filters and
public static Bson and(final Bson... filters)
From source file:org.opencb.cellbase.mongodb.impl.ProteinMongoDBAdaptor.java
License:Apache License
private Bson parseQuery(Query query) { List<Bson> andBsonList = new ArrayList<>(); createOrQuery(query, QueryParams.ACCESSION.key(), "accession", andBsonList); createOrQuery(query, QueryParams.NAME.key(), "name", andBsonList); createOrQuery(query, QueryParams.GENE.key(), "gene", andBsonList); createOrQuery(query, QueryParams.XREFS.key(), "dbReference.id", andBsonList); createOrQuery(query, QueryParams.KEYWORD.key(), "keyword", andBsonList); createOrQuery(query, QueryParams.FEATURE_ID.key(), "feature.id", andBsonList); createOrQuery(query, QueryParams.FEATURE_TYPE.key(), "feature.type", andBsonList); if (andBsonList.size() > 0) { return Filters.and(andBsonList); } else {// www . jav a2s. c o m return new Document(); } }
From source file:org.opencb.cellbase.mongodb.impl.VariantMongoDBAdaptor.java
License:Apache License
private Bson parseQuery(Query query) { List<Bson> andBsonList = new ArrayList<>(); createRegionQuery(query, VariantMongoDBAdaptor.QueryParams.REGION.key(), MongoDBCollectionConfiguration.VARIATION_CHUNK_SIZE, andBsonList); createOrQuery(query, VariantMongoDBAdaptor.QueryParams.ID.key(), "id", andBsonList); createOrQuery(query, VariantMongoDBAdaptor.QueryParams.GENE.key(), "transcriptVariations.transcriptId", andBsonList);/*from www .j a v a2 s .c om*/ createOrQuery(query, QueryParams.CHROMOSOME.key(), "chromosome", andBsonList); createOrQuery(query, QueryParams.REFERENCE.key(), "reference", andBsonList); createOrQuery(query, QueryParams.ALTERNATE.key(), "alternate", andBsonList); createOrQuery(query, VariantMongoDBAdaptor.QueryParams.CONSEQUENCE_TYPE.key(), "consequenceTypes", andBsonList); createOrQuery(query, VariantMongoDBAdaptor.QueryParams.XREFS.key(), "transcripts.xrefs.id", andBsonList); if (andBsonList.size() > 0) { return Filters.and(andBsonList); } else { return new Document(); } }
From source file:org.opencb.commons.datastore.mongodb.MongoDBQueryUtils.java
License:Apache License
public static Bson createAutoFilter(String mongoDbField, String queryParam, Query query, QueryParam.Type type, LogicalOperator operator) throws NumberFormatException { List<String> queryParamList = query.getAsStringList(queryParam, getLogicalSeparator(operator)); if (LogicalOperator.OR.equals(operator) && queryParamsOperatorAlwaysMatchesOperator(type, queryParamList, ComparisonOperator.EQUALS)) { // It is better to perform a $in operation return Filters.in(mongoDbField, removeOperatorsFromQueryParamList(type, queryParamList)); } else if (LogicalOperator.AND.equals(operator) && queryParamsOperatorAlwaysMatchesOperator(type, queryParamList, ComparisonOperator.NOT_EQUALS)) { // It is better to perform a $nin operation return Filters.nin(mongoDbField, removeOperatorsFromQueryParamList(type, queryParamList)); } else {//from w w w . j a va 2s .com List<Bson> bsonList = new ArrayList<>(queryParamList.size()); for (String queryItem : queryParamList) { Matcher matcher = getPattern(type).matcher(queryItem); String op = ""; String queryValueString = queryItem; if (matcher.find()) { op = matcher.group(1); queryValueString = matcher.group(2); } ComparisonOperator comparator = getComparisonOperator(op, type); switch (type) { case STRING: case TEXT: case TEXT_ARRAY: bsonList.add(createFilter(mongoDbField, queryValueString, comparator)); break; case LONG: case LONG_ARRAY: case INTEGER: case INTEGER_ARRAY: bsonList.add(createFilter(mongoDbField, Long.parseLong(queryValueString), comparator)); break; case DOUBLE: case DECIMAL: case DECIMAL_ARRAY: bsonList.add(createFilter(mongoDbField, Double.parseDouble(queryValueString), comparator)); break; case BOOLEAN: case BOOLEAN_ARRAY: bsonList.add(createFilter(mongoDbField, Boolean.parseBoolean(queryValueString), comparator)); break; case DATE: case TIMESTAMP: List<String> dateList = new ArrayList<>(); dateList.add(queryValueString); if (!matcher.group(3).isEmpty()) { dateList.add(matcher.group(4)); comparator = ComparisonOperator.BETWEEN; } bsonList.add(createDateFilter(mongoDbField, dateList, comparator, type)); break; default: break; } } Bson filter; if (bsonList.size() == 0) { filter = Filters.size(queryParam, 0); } else if (bsonList.size() == 1) { filter = bsonList.get(0); } else { if (operator.equals(LogicalOperator.OR)) { filter = Filters.or(bsonList); } else { filter = Filters.and(bsonList); } } return filter; } }
From source file:org.opencb.commons.datastore.mongodb.MongoDBQueryUtils.java
License:Apache License
public static <T> Bson createFilter(String mongoDbField, List<T> queryValues, ComparisonOperator comparator, LogicalOperator operator) {/*from ww w . ja v a2 s .c o m*/ Bson filter = null; if (queryValues != null && queryValues.size() > 0) { if (comparator.equals(ComparisonOperator.IN) || comparator.equals(ComparisonOperator.NOT_IN) || comparator.equals(ComparisonOperator.ALL)) { switch (comparator) { case IN: filter = Filters.in(mongoDbField, queryValues); break; case NOT_IN: filter = Filters.nin(mongoDbField, queryValues); break; case ALL: filter = Filters.all(mongoDbField, queryValues); break; default: break; } } else { // If there is only on element in the array then it does not make sense to create an OR or AND filter if (queryValues.size() == 1) { filter = createFilter(mongoDbField, queryValues.get(0), comparator); } else { List<Bson> bsonList = new ArrayList<>(queryValues.size()); for (T queryItem : queryValues) { Bson filter1 = createFilter(mongoDbField, queryItem, comparator); if (filter1 != null) { bsonList.add(filter1); } } if (operator.equals(LogicalOperator.OR)) { filter = Filters.or(bsonList); } else { filter = Filters.and(bsonList); } } } } return filter; }
From source file:org.opencb.opencga.catalog.db.mongodb.CatalogMongoProjectDBAdaptor.java
License:Apache License
private Bson parseQuery(Query query) throws CatalogDBException { List<Bson> andBsonList = new ArrayList<>(); for (Map.Entry<String, Object> entry : query.entrySet()) { String key = entry.getKey().split("\\.")[0]; QueryParams queryParam = QueryParams.getParam(entry.getKey()) != null ? QueryParams.getParam(entry.getKey()) : QueryParams.getParam(key); try {//from w ww. jav a2 s . co m switch (queryParam) { case ID: addOrQuery("projects." + queryParam.key(), queryParam.key(), query, queryParam.type(), andBsonList); break; case USER_ID: addOrQuery(PRIVATE_ID, queryParam.key(), query, queryParam.type(), andBsonList); break; case ATTRIBUTES: addAutoOrQuery("projects." + entry.getKey(), entry.getKey(), query, queryParam.type(), andBsonList); break; case BATTRIBUTES: String mongoKey = "projects." + entry.getKey().replace(QueryParams.BATTRIBUTES.key(), QueryParams.ATTRIBUTES.key()); addAutoOrQuery(mongoKey, entry.getKey(), query, queryParam.type(), andBsonList); break; case NATTRIBUTES: mongoKey = "projects." + entry.getKey().replace(QueryParams.NATTRIBUTES.key(), QueryParams.ATTRIBUTES.key()); addAutoOrQuery(mongoKey, entry.getKey(), query, queryParam.type(), andBsonList); break; default: addAutoOrQuery("projects." + queryParam.key(), queryParam.key(), query, queryParam.type(), andBsonList); break; } } catch (Exception e) { throw new CatalogDBException(e); } } if (andBsonList.size() > 0) { return Filters.and(andBsonList); } else { return new Document(); } }
From source file:org.opencb.opencga.catalog.db.mongodb.CatalogMongoStudyDBAdaptor.java
License:Apache License
@Deprecated @Override/*from w w w.ja v a 2s . co m*/ public QueryResult<Role> getRole(long studyId, String userId, String groupId, String roleId, QueryOptions options) throws CatalogDBException { long startTime = startQuery(); Bson query = new Document(PRIVATE_ID, studyId); List<Bson> roleQuery = new ArrayList<>(); List<String> userIds = new ArrayList<>(); if (userId != null) { userIds.add(userId); } if (groupId != null) { userIds.add(groupId); } if (userIds.size() > 0) { roleQuery.add(Filters.in("userIds", userIds)); } if (roleId != null) { roleQuery.add(Filters.eq("id", roleId)); } Bson projection = Projections.elemMatch(QueryParams.ROLES.key(), Filters.and(roleQuery)); QueryResult<Document> queryResult = studyCollection.find(query, projection, filterOptions(options, FILTER_ROUTE_STUDIES + QueryParams.ROLES.key() + ".")); List<Study> studies = CatalogMongoDBUtils.parseStudies(queryResult); List<Role> roles = new ArrayList<>(1); // studies.stream().filter(study -> study.getRoles() != null).forEach(study -> { // roles.addAll(study.getRoles()); // }); return endQuery("getRole", startTime, roles); }
From source file:org.opencb.opencga.catalog.db.mongodb.CatalogMongoStudyDBAdaptor.java
License:Apache License
@Override public QueryResult<VariableSet> getAllVariableSets(long studyId, QueryOptions options) throws CatalogDBException { long startTime = startQuery(); List<Bson> mongoQueryList = new LinkedList<>(); for (Map.Entry<String, Object> entry : options.entrySet()) { String key = entry.getKey().split("\\.")[0]; try {/*from w ww. j a v a 2 s . co m*/ if (isDataStoreOption(key) || isOtherKnownOption(key)) { continue; //Exclude DataStore options } CatalogStudyDBAdaptor.VariableSetParams option = CatalogStudyDBAdaptor.VariableSetParams .getParam(key) != null ? CatalogStudyDBAdaptor.VariableSetParams.getParam(key) : CatalogStudyDBAdaptor.VariableSetParams.getParam(entry.getKey()); switch (option) { case STUDY_ID: addCompQueryFilter(option, option.name(), PRIVATE_ID, options, mongoQueryList); break; default: String optionsKey = "variableSets." + entry.getKey().replaceFirst(option.name(), option.key()); addCompQueryFilter(option, entry.getKey(), optionsKey, options, mongoQueryList); break; } } catch (IllegalArgumentException e) { throw new CatalogDBException(e); } } /* QueryResult<DBObject> queryResult = studyCollection.aggregate(Arrays.<DBObject>asList( new BasicDBObject("$match", new BasicDBObject(PRIVATE_ID, studyId)), new BasicDBObject("$project", new BasicDBObject("variableSets", 1)), new BasicDBObject("$unwind", "$variableSets"), new BasicDBObject("$match", new BasicDBObject("$and", mongoQueryList)) ), filterOptions(options, FILTER_ROUTE_STUDIES)); */ List<Bson> aggregation = new ArrayList<>(); aggregation.add(Aggregates.match(Filters.eq(PRIVATE_ID, studyId))); aggregation.add(Aggregates.project(Projections.include("variableSets"))); aggregation.add(Aggregates.unwind("$variableSets")); if (mongoQueryList.size() > 0) { aggregation.add(Aggregates.match(Filters.and(mongoQueryList))); } QueryResult<Document> queryResult = studyCollection.aggregate(aggregation, filterOptions(options, FILTER_ROUTE_STUDIES)); List<VariableSet> variableSets = parseObjects(queryResult, Study.class).stream() .map(study -> study.getVariableSets().get(0)).collect(Collectors.toList()); return endQuery("", startTime, variableSets); }
From source file:org.opencb.opencga.catalog.db.mongodb.CatalogMongoStudyDBAdaptor.java
License:Apache License
private Bson parseQuery(Query query) throws CatalogDBException { List<Bson> andBsonList = new ArrayList<>(); for (Map.Entry<String, Object> entry : query.entrySet()) { String key = entry.getKey().split("\\.")[0]; QueryParams queryParam = QueryParams.getParam(entry.getKey()) != null ? QueryParams.getParam(entry.getKey()) : QueryParams.getParam(key); try {//from w ww .j av a2 s.com switch (queryParam) { case ID: addOrQuery(PRIVATE_ID, queryParam.key(), query, queryParam.type(), andBsonList); break; case PROJECT_ID: addOrQuery(PRIVATE_PROJECT_ID, queryParam.key(), query, queryParam.type(), andBsonList); break; case ATTRIBUTES: addAutoOrQuery(entry.getKey(), entry.getKey(), query, queryParam.type(), andBsonList); break; case BATTRIBUTES: String mongoKey = entry.getKey().replace(QueryParams.BATTRIBUTES.key(), QueryParams.ATTRIBUTES.key()); addAutoOrQuery(mongoKey, entry.getKey(), query, queryParam.type(), andBsonList); break; case NATTRIBUTES: mongoKey = entry.getKey().replace(QueryParams.NATTRIBUTES.key(), QueryParams.ATTRIBUTES.key()); addAutoOrQuery(mongoKey, entry.getKey(), query, queryParam.type(), andBsonList); break; default: addAutoOrQuery(queryParam.key(), queryParam.key(), query, queryParam.type(), andBsonList); break; } } catch (Exception e) { throw new CatalogDBException(e); } } if (andBsonList.size() > 0) { return Filters.and(andBsonList); } else { return new Document(); } }
From source file:org.opencb.opencga.catalog.db.mongodb.IndividualMongoDBAdaptor.java
License:Apache License
private Bson parseQuery(Query query) throws CatalogDBException { List<Bson> andBsonList = new ArrayList<>(); List<Bson> annotationList = new ArrayList<>(); // We declare variableMap here just in case we have different annotation queries Map<String, Variable> variableMap = null; if (query.containsKey(QueryParams.ANNOTATION.key())) { fixAnnotationQuery(query);/*ww w . j av a 2s . c o m*/ } for (Map.Entry<String, Object> entry : query.entrySet()) { String key = entry.getKey().split("\\.")[0]; QueryParams queryParam = QueryParams.getParam(entry.getKey()) != null ? QueryParams.getParam(entry.getKey()) : QueryParams.getParam(key); try { switch (queryParam) { case ID: addOrQuery(PRIVATE_ID, queryParam.key(), query, queryParam.type(), andBsonList); break; case STUDY_ID: addOrQuery(PRIVATE_STUDY_ID, queryParam.key(), query, queryParam.type(), andBsonList); break; case ATTRIBUTES: addAutoOrQuery(entry.getKey(), entry.getKey(), query, queryParam.type(), andBsonList); break; case BATTRIBUTES: String mongoKey = entry.getKey().replace(QueryParams.BATTRIBUTES.key(), QueryParams.ATTRIBUTES.key()); addAutoOrQuery(mongoKey, entry.getKey(), query, queryParam.type(), andBsonList); break; case NATTRIBUTES: mongoKey = entry.getKey().replace(QueryParams.NATTRIBUTES.key(), QueryParams.ATTRIBUTES.key()); addAutoOrQuery(mongoKey, entry.getKey(), query, queryParam.type(), andBsonList); break; case VARIABLE_SET_ID: addOrQuery(queryParam.key(), queryParam.key(), query, queryParam.type(), annotationList); break; case ANNOTATION: if (variableMap == null) { long variableSetId = query.getLong(QueryParams.VARIABLE_SET_ID.key()); variableMap = dbAdaptorFactory.getCatalogStudyDBAdaptor() .getVariableSet(variableSetId, null).first().getVariables().stream() .collect(Collectors.toMap(Variable::getName, Function.identity())); } addAnnotationQueryFilter(entry.getKey(), query, variableMap, annotationList); break; case ANNOTATION_SET_NAME: addOrQuery("name", queryParam.key(), query, queryParam.type(), annotationList); break; default: addAutoOrQuery(queryParam.key(), queryParam.key(), query, queryParam.type(), andBsonList); break; } } catch (Exception e) { throw new CatalogDBException(e); } } if (annotationList.size() > 0) { Bson projection = Projections.elemMatch(QueryParams.ANNOTATION_SETS.key(), Filters.and(annotationList)); andBsonList.add(projection); } if (andBsonList.size() > 0) { return Filters.and(andBsonList); } else { return new Document(); } }
From source file:org.opencb.opencga.catalog.db.mongodb.MongoDBUtils.java
License:Apache License
static QueryResult<Document> getAcl(long id, List<String> members, MongoDBCollection collection, Logger logger) throws CatalogDBException { List<Bson> aggregation = new ArrayList<>(); aggregation.add(Aggregates.match(Filters.eq(PRIVATE_ID, id))); aggregation.add(Aggregates.project(//from w ww .ja v a 2s . c o m Projections.include(FileDBAdaptor.QueryParams.ID.key(), FileDBAdaptor.QueryParams.ACL.key()))); aggregation.add(Aggregates.unwind("$" + FileDBAdaptor.QueryParams.ACL.key())); List<Bson> filters = new ArrayList<>(); if (members != null && members.size() > 0) { filters.add(Filters.in(FileDBAdaptor.QueryParams.ACL_MEMBER.key(), members)); } if (filters.size() > 0) { Bson filter = filters.size() == 1 ? filters.get(0) : Filters.and(filters); aggregation.add(Aggregates.match(filter)); } for (Bson bson : aggregation) { logger.debug("Get Acl: {}", bson.toBsonDocument(Document.class, com.mongodb.MongoClient.getDefaultCodecRegistry())); } return collection.aggregate(aggregation, null); }