public static <T> Bson createFilter(String mongoDbField, List<T> queryValues, LogicalOperator operator) { return createFilter(mongoDbField, queryValues, ComparisonOperator.EQUALS, operator); }
public static void parseQueryOptions(List<Bson> operations, QueryOptions options) { if (options != null) { Bson projection = getProjection(options); if (projection != null) { operations.add(projection); } Bson skip = getSkip(options); if (skip != null) { operations.add(skip); } Bson limit = getLimit(options); if (limit != null) { operations.add(limit); } Bson sort = getSort(options); if (sort != null) { operations.add(sort); } } }
List<String> queryParamList = query.getAsStringList(queryParam, getLogicalSeparator(operator)); && queryParamsOperatorAlwaysMatchesOperator(type, queryParamList, ComparisonOperator.EQUALS)) { return Filters.in(mongoDbField, removeOperatorsFromQueryParamList(type, queryParamList)); } else if (LogicalOperator.AND.equals(operator) && queryParamsOperatorAlwaysMatchesOperator(type, queryParamList, ComparisonOperator.NOT_EQUALS)) { return Filters.nin(mongoDbField, removeOperatorsFromQueryParamList(type, queryParamList)); } else { List<Bson> bsonList = new ArrayList<>(queryParamList.size()); for (String queryItem : queryParamList) { Matcher matcher = getPattern(type).matcher(queryItem); String op = ""; String queryValueString = queryItem; ComparisonOperator comparator = getComparisonOperator(op, type); switch (type) { case STRING: case TEXT: case TEXT_ARRAY: bsonList.add(createFilter(mongoDbField, queryValueString, comparator)); break; case LONG: case INTEGER: case INTEGER_ARRAY: bsonList.add(createFilter(mongoDbField, Long.parseLong(queryValueString), comparator)); break;
case TEXT: case TEXT_ARRAY: filter = createFilter(mongoDbField, query.getAsStringList(queryParam, getLogicalSeparator(operator)), comparator, operator); break; case LONG: case LONG_ARRAY: filter = createFilter(mongoDbField, query.getAsLongList(queryParam, getLogicalSeparator(operator)), comparator, operator); break; case DECIMAL: case DECIMAL_ARRAY: filter = createFilter(mongoDbField, query.getAsDoubleList(queryParam, getLogicalSeparator(operator)), comparator, operator); break; case BOOLEAN: case BOOLEAN_ARRAY: filter = createFilter(mongoDbField, query.getBoolean(queryParam), comparator); break; case DATE: case TIMESTAMP: filter = createDateFilter(mongoDbField, query.getAsStringList(queryParam), comparator, type); break; default:
public static Bson createAutoFilter(String mongoDbField, String queryParam, Query query, QueryParam.Type type) throws NumberFormatException { Bson filter = null; if (query != null && query.containsKey(queryParam)) { List<String> values = query.getAsStringList(queryParam); LogicalOperator operator = LogicalOperator.OR; if (values.size() == 1) { operator = checkOperator(values.get(0)); } filter = createAutoFilter(mongoDbField, queryParam, query, type, operator); } return filter; }
/** * Auxiliary method to check if the operator of each of the values in the queryParamList matches the operator passed. * * @param type QueryParam type. * @param queryParamList List of values. * @param operator Operator to be checked. * @return boolean indicating whether the list of values have always the same operator or not. */ private static boolean queryParamsOperatorAlwaysMatchesOperator(QueryParam.Type type, List<String> queryParamList, ComparisonOperator operator) { for (String queryItem : queryParamList) { Matcher matcher = getPattern(type).matcher(queryItem); String op = ""; if (matcher.find()) { op = matcher.group(1); } if (operator != getComparisonOperator(op, type)) { return false; } } return true; }
/** * It will check for the proper comparator based on the query value and create the correct query filter. * It could be a regular expression, >, < ... or a simple equals. * @param mongoDbField The field used in the mongoDB. * @param queryParam The key by which the parameter is stored in the query. Normally, it will be the same as in the data model, * although it might be some exceptions. * @param query The object containing the key:values of the query. * @param paramType The type of the object to be looked up. See {@link QueryParam}. * @param andBsonList The list where created filter will be added to. */ protected void addAutoOrQuery(String mongoDbField, String queryParam, Query query, QueryParam.Type paramType, List<Bson> andBsonList) { if (query != null && query.getString(queryParam) != null) { Bson filter = MongoDBQueryUtils.createAutoFilter(mongoDbField, queryParam, query, paramType); if (filter != null) { andBsonList.add(filter); } } }
public static List<Bson> createGroupBy(Bson query, String groupByField, String idField, boolean count) { if (groupByField == null || groupByField.isEmpty()) { return new ArrayList<>(); } if (groupByField.contains(",")) { // call to multiple createGroupBy if commas are present return createGroupBy(query, Arrays.asList(groupByField.split(",")), idField, count); } else { Bson match = Aggregates.match(query); Bson project = Aggregates.project(Projections.include(groupByField, idField)); Bson group; if (count) { group = Aggregates.group("$" + groupByField, Accumulators.sum("count", 1)); } else { group = Aggregates.group("$" + groupByField, Accumulators.addToSet("features", "$" + idField)); } return Arrays.asList(match, project, group); } }
Matcher matcher = getPattern(type).matcher(queryItem); String queryValueString = queryItem; if (matcher.find()) {
date = convertStringToDate(dateValues.get(0)); } else if (QueryParam.Type.TIMESTAMP.equals(type)) { date = convertStringToDate(dateValues.get(0)).getTime(); case BETWEEN: if (dateValues.size() == 2) { Date to = convertStringToDate(dateValues.get(1));
public static List<Bson> createGroupBy(Bson query, List<String> groupByField, String idField, boolean count) { if (groupByField == null || groupByField.isEmpty()) { return new ArrayList<>(); } if (groupByField.size() == 1) { // if only one field then we call to simple createGroupBy return createGroupBy(query, groupByField.get(0), idField, count); } else { Bson match = Aggregates.match(query); // add all group-by fields to the projection together with the aggregation field name List<String> groupByFields = new ArrayList<>(groupByField); groupByFields.add(idField); Bson project = Aggregates.project(Projections.include(groupByFields)); // _id document creation to have the multiple id Document id = new Document(); for (String s : groupByField) { id.append(s, "$" + s); } Bson group; if (count) { group = Aggregates.group(id, Accumulators.sum("count", 1)); } else { group = Aggregates.group(id, Accumulators.addToSet("features", "$" + idField)); } return Arrays.asList(match, project, group); } }
public static Bson createFilter(String mongoDbField, String queryParam, Query query) { return createFilter(mongoDbField, queryParam, query, QueryParam.Type.TEXT, ComparisonOperator.EQUALS, LogicalOperator.OR); }
public static <T> Bson createFilter(String mongoDbField, List<T> queryValues, ComparisonOperator comparator) { return createFilter(mongoDbField, queryValues, comparator, LogicalOperator.OR); }
public static Bson createFilter(String mongoDbField, String queryParam, Query query, QueryParam.Type type, ComparisonOperator comparator) { return createFilter(mongoDbField, queryParam, query, type, comparator, LogicalOperator.OR); }
public static <T> Bson createFilter(String mongoDbField, T queryValue) { return createFilter(mongoDbField, queryValue, ComparisonOperator.EQUALS); }
public static Bson createFilter(String mongoDbField, String queryParam, Query query, QueryParam.Type type) { return createFilter(mongoDbField, queryParam, query, type, ComparisonOperator.EQUALS, LogicalOperator.OR); }
public static <T> Bson createFilter(String mongoDbField, List<T> queryValues) { return createFilter(mongoDbField, queryValues, ComparisonOperator.EQUALS, LogicalOperator.OR); }
protected void addQueryFilter(String mongoDbField, String queryParam, Query query, QueryParam.Type paramType, MongoDBQueryUtils.ComparisonOperator comparisonOperator, MongoDBQueryUtils.LogicalOperator operator, List<Bson> andBsonList) { if (query != null && query.getString(queryParam) != null) { Bson filter = MongoDBQueryUtils.createFilter(mongoDbField, queryParam, query, paramType, comparisonOperator, operator); if (filter != null) { andBsonList.add(filter); } } }
/** * Parse the query values from the ontology terms to bson format. * At this point, any value that we find in the query (as comma separated or as a list), will be looked for in the id and name * attributes of the ontologyTerm java bean. * * Example: ontologyTerms: X,Y * This will be transformed to something like ontologyTerms.id == X || ontologyTerms.name == X || ontologyTerms.id == Y || * ontologyTerms.name == Y) * * @param mongoKey Key corresponding to the data model to know how it is stored in mongoDB. (If not altered, ontologyTerms). * @param queryKey Key by which the values will be retrieved from the query. * @param query Query object containing all the query keys and values to parse. Only to get the ones regarding ontology terms. * @param bsonList List to which we will add the ontology terms search. */ public static void addOntologyQueryFilter(String mongoKey, String queryKey, Query query, List<Bson> bsonList) { List<String> ontologyValues = query.getAsStringList(queryKey); Bson ontologyId = MongoDBQueryUtils.createFilter(mongoKey + ".id", ontologyValues); Bson ontologyName = MongoDBQueryUtils.createFilter(mongoKey + ".name", ontologyValues); bsonList.add(Filters.or(ontologyId, ontologyName)); }
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);