Example usage for com.mongodb.client.model Filters elemMatch

List of usage examples for com.mongodb.client.model Filters elemMatch

Introduction

In this page you can find the example usage for com.mongodb.client.model Filters elemMatch.

Prototype

public static Bson elemMatch(final String fieldName, final Bson filter) 

Source Link

Document

Creates a filter that matches all documents containing a field that is an array where at least one member of the array matches the given filter.

Usage

From source file:io.lumeer.storage.mongodb.dao.system.MongoUserDao.java

License:Open Source License

private Bson organizationIdFilter(final String organizationId) {
    return Filters.elemMatch(UserCodec.ALL_GROUPS, Filters.eq(UserCodec.ORGANIZATION_ID, organizationId));
}

From source file:io.lumeer.storage.mongodb.util.MongoFilters.java

License:Open Source License

private static Bson entityPermissionsFilter(String entityField, String entityName) {
    return Filters.elemMatch(PERMISSIONS + "." + entityField,
            Filters.and(entityNameFilter(entityName), entityRolesFilter(Role.READ)));
}

From source file:org.eclipse.ditto.services.thingsearch.persistence.read.expression.visitors.GetExistsBsonVisitor.java

License:Open Source License

@Override
Bson visitPointer(final String pointer) {
    return getAuthorizationBson()
            .map(authBson -> Filters.elemMatch(FIELD_INTERNAL,
                    Filters.and(authBson, Filters.eq(FIELD_INTERNAL_KEY, pointer))))
            .orElseGet(() -> Filters.eq(FIELD_PATH_KEY, pointer));
}

From source file:org.eclipse.ditto.services.thingsearch.persistence.read.expression.visitors.GetExistsBsonVisitor.java

License:Open Source License

private Bson matchKey(final String keyRegex) {
    return getAuthorizationBson()
            .map(authBson -> Filters.elemMatch(FIELD_INTERNAL,
                    Filters.and(authBson, Filters.regex(FIELD_INTERNAL_KEY, keyRegex))))
            .orElseGet(() -> Filters.regex(PersistenceConstants.FIELD_PATH_KEY, keyRegex));
}

From source file:org.eclipse.ditto.services.thingsearch.persistence.read.expression.visitors.GetFilterBsonVisitor.java

License:Open Source License

private Bson matchKeyValue(final String key) {
    final Bson keyValueFilter = Filters.and(Filters.eq(FIELD_INTERNAL_KEY, key), valueFilter);
    return Filters.elemMatch(FIELD_INTERNAL, getAuthorizationBson()
            .map(authBson -> Filters.and(keyValueFilter, authBson)).orElse(keyValueFilter));
}

From source file:org.opencb.opencga.catalog.db.mongodb.CatalogMongoStudyDBAdaptor.java

License:Apache License

@Override
public Long variableSetExists(long variableSetId) {
    List<Bson> aggregation = new ArrayList<>();
    aggregation.add(Aggregates.match(Filters.elemMatch(QueryParams.VARIABLE_SET.key(),
            Filters.eq(VariableSetParams.ID.key(), variableSetId))));
    aggregation.add(Aggregates.project(Projections.include(QueryParams.VARIABLE_SET.key())));
    aggregation.add(Aggregates.unwind("$" + QueryParams.VARIABLE_SET.key()));
    aggregation.add(Aggregates.match(Filters.eq(QueryParams.VARIABLE_SET_ID.key(), variableSetId)));
    QueryResult<VariableSet> queryResult = studyCollection.aggregate(aggregation, variableSetConverter,
            new QueryOptions());

    return (long) queryResult.getResult().size();
}

From source file:org.opencb.opencga.catalog.db.mongodb.CatalogMongoStudyDBAdaptor.java

License:Apache License

/**
 * The method will return the variable object given variableSetId and the variableId.
 * @param variableSetId Id of the variableSet.
 * @param variableId Id of the variable inside the variableSet.
 * @return the variable object./*from   w w w  .j av a2  s. c  o m*/
 */
private Variable getVariable(long variableSetId, String variableId) throws CatalogDBException {
    List<Bson> aggregation = new ArrayList<>();
    aggregation.add(Aggregates.match(Filters.elemMatch(QueryParams.VARIABLE_SET.key(),
            Filters.eq(VariableSetParams.ID.key(), variableSetId))));
    aggregation.add(Aggregates.project(Projections.include(QueryParams.VARIABLE_SET.key())));
    aggregation.add(Aggregates.unwind("$" + QueryParams.VARIABLE_SET.key()));
    aggregation.add(Aggregates.match(Filters.eq(QueryParams.VARIABLE_SET_ID.key(), variableSetId)));
    aggregation.add(
            Aggregates.unwind("$" + QueryParams.VARIABLE_SET.key() + "." + VariableSetParams.VARIABLE.key()));
    aggregation.add(Aggregates.match(Filters
            .eq(QueryParams.VARIABLE_SET.key() + "." + VariableSetParams.VARIABLE_NAME.key(), variableId)));

    QueryResult<Document> queryResult = studyCollection.aggregate(aggregation, new QueryOptions());

    Document variableSetDocument = (Document) queryResult.first().get(QueryParams.VARIABLE_SET.key());
    VariableSet variableSet = variableSetConverter.convertToDataModelType(variableSetDocument);
    Iterator<Variable> iterator = variableSet.getVariables().iterator();
    if (iterator.hasNext()) {
        return iterator.next();
    } else {
        // This error should never be raised.
        throw new CatalogDBException(
                "VariableSet {id: " + variableSetId + "} - Could not obtain variable object.");
    }
}

From source file:org.opencb.opencga.catalog.db.mongodb.CatalogMongoStudyDBAdaptor.java

License:Apache License

/**
 * Checks if the variable given is present in the variableSet.
 * @param variableSetId Identifier of the variableSet where it will be checked.
 * @param variableId VariableId that will be checked.
 * @throws CatalogDBException when the variableId is not present in the variableSet.
 *///from  w w w  .  j  av a2s  .co m
private void checkVariableInVariableSet(long variableSetId, String variableId) throws CatalogDBException {
    List<Bson> aggregation = new ArrayList<>();
    aggregation.add(Aggregates.match(Filters.elemMatch(QueryParams.VARIABLE_SET.key(),
            Filters.and(Filters.eq(VariableSetParams.ID.key(), variableSetId),
                    Filters.eq(VariableSetParams.VARIABLE_NAME.key(), variableId)))));

    if (studyCollection.aggregate(aggregation, new QueryOptions()).getNumResults() == 0) {
        throw new CatalogDBException("VariableSet {id: " + variableSetId + "}. The variable {id: " + variableId
                + "} does not exist.");
    }
}

From source file:org.opencb.opencga.catalog.db.mongodb.CatalogMongoStudyDBAdaptor.java

License:Apache License

/**
 * Checks if the variable given is not present in the variableSet.
 * @param variableSetId Identifier of the variableSet where it will be checked.
 * @param variableId VariableId that will be checked.
 * @throws CatalogDBException when the variableId is present in the variableSet.
 *//*  w w  w  .ja  va 2 s . c  om*/
private void checkVariableNotInVariableSet(long variableSetId, String variableId) throws CatalogDBException {
    List<Bson> aggregation = new ArrayList<>();
    aggregation.add(Aggregates.match(Filters.elemMatch(QueryParams.VARIABLE_SET.key(),
            Filters.and(Filters.eq(VariableSetParams.ID.key(), variableSetId),
                    Filters.ne(VariableSetParams.VARIABLE_NAME.key(), variableId)))));

    if (studyCollection.aggregate(aggregation, new QueryOptions()).getNumResults() == 0) {
        throw new CatalogDBException("VariableSet {id: " + variableSetId + "}. The variable {id: " + variableId
                + "} already exists.");
    }
}

From source file:org.opencb.opencga.catalog.db.mongodb.MongoDBUtils.java

License:Apache License

public static void addAnnotationQueryFilter(String optionKey, Query query, Map<String, Variable> variableMap,
        List<Bson> annotationSetFilter) throws CatalogDBException {
    // Annotation Filter
    final String sepOr = ",";

    String annotationKey;/*from  w  ww  .ja va 2 s  .co  m*/
    if (optionKey.startsWith("annotation.")) {
        annotationKey = optionKey.substring("annotation.".length());
    } else {
        throw new CatalogDBException(
                "Wrong annotation query. Expects: {\"annotation.<variable>\" , <operator><value> } ");
    }
    String annotationValue = query.getString(optionKey);

    final String variableId;
    final String route;
    if (annotationKey.contains(".")) {
        String[] variableIdRoute = annotationKey.split("\\.", 2);
        variableId = variableIdRoute[0];
        route = "." + variableIdRoute[1];
    } else {
        variableId = annotationKey;
        route = "";
    }
    String[] values = annotationValue.split(sepOr);

    QueryParam.Type type = QueryParam.Type.TEXT;

    if (variableMap != null) {
        Variable variable = variableMap.get(variableId);
        if (variable == null) {
            throw new CatalogDBException("Variable \"" + variableId + "\" not found in variableSet ");
        }
        Variable.VariableType variableType = variable.getType();
        if (variable.getType() == Variable.VariableType.OBJECT) {
            String[] routes = route.split("\\.");
            for (String r : routes) {
                if (variable.getType() != Variable.VariableType.OBJECT) {
                    throw new CatalogDBException("Unable to query variable " + annotationKey);
                }
                if (variable.getVariableSet() != null) {
                    Map<String, Variable> subVariableMap = variable.getVariableSet().stream()
                            .collect(Collectors.toMap(Variable::getName, Function.<Variable>identity()));
                    if (subVariableMap.containsKey(r)) {
                        variable = subVariableMap.get(r);
                        variableType = variable.getType();
                    }
                } else {
                    variableType = Variable.VariableType.TEXT;
                    break;
                }
            }
        }
        if (variableType == Variable.VariableType.BOOLEAN) {
            type = QueryParam.Type.BOOLEAN;

        } else if (variableType == Variable.VariableType.NUMERIC) {
            type = QueryParam.Type.DECIMAL;
        }
    }

    List<Bson> valueList = addCompQueryFilter(type, "value" + route, Arrays.asList(values), new ArrayList<>());
    annotationSetFilter.add(
            Filters.elemMatch("annotations", Filters.and(Filters.eq("name", variableId), valueList.get(0))));
}