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

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

Introduction

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

Prototype

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

Source Link

Document

Creates a projection that includes for the given field only the first element of the array value of that field that matches the given query filter.

Usage

From source file:mongodb.clients.percunia.mongo.Projection.java

License:Apache License

public static Bson elemMatch(String field, Bson restriction) {
    return Projections.elemMatch(field, restriction);
}

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

License:Apache License

@Override
public long getProjectId(String userId, String projectAlias) throws CatalogDBException {
    QueryResult<Document> queryResult = userCollection.find(
            new BsonDocument("projects.alias", new BsonString(projectAlias)).append("id",
                    new BsonString(userId)),
            Projections.fields(Projections.include("projects.id"),
                    Projections.elemMatch("projects", Filters.eq("alias", projectAlias))),
            null);/*w  ww . j  a v  a 2  s. c o m*/
    /*
            QueryResult<DBObject> queryResult = userCollection.find(
        BasicDBObjectBuilder
                .start("projects.alias", projectAlias)
                .append("id", userId).get(),
        BasicDBObjectBuilder.start("projects.id", true)
                .append("projects", new BasicDBObject("$elemMatch", new BasicDBObject("alias", projectAlias))).get(),
        null
            );*/
    User user = parseUser(queryResult);
    if (user == null || user.getProjects().isEmpty()) {
        return -1;
    } else {
        return user.getProjects().get(0).getId();
    }
}

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

License:Apache License

@Deprecated
@Override//from ww  w .j ava 2 s.  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> getVariableSet(long variableSetId, QueryOptions options)
        throws CatalogDBException {
    long startTime = startQuery();

    Query query = new Query(QueryParams.VARIABLE_SET_ID.key(), variableSetId);
    Bson projection = Projections.elemMatch("variableSets", Filters.eq("id", variableSetId));
    if (options == null) {
        options = new QueryOptions();
    }//from w w w .  j av  a2s  .co  m
    QueryOptions qOptions = new QueryOptions(options);
    qOptions.put(MongoDBCollection.ELEM_MATCH, projection);
    QueryResult<Study> studyQueryResult = get(query, qOptions);

    if (studyQueryResult.getResult().isEmpty() || studyQueryResult.first().getVariableSets().isEmpty()) {
        throw new CatalogDBException("VariableSet {id: " + variableSetId + "} does not exist.");
    }

    /*
            Bson query = Filters.eq("variableSets.id", variableSetId);
            Bson projection = Projections.elemMatch("variableSets", Filters.eq("id", variableSetId));
            QueryOptions filteredOptions = filterOptions(options, FILTER_ROUTE_STUDIES);
            
            QueryResult<Document> queryResult = studyCollection.find(query, projection, filteredOptions);
            List<Study> studies = parseStudies(queryResult);
            if (studies.isEmpty() || studies.get(0).getVariableSets().isEmpty()) {
    throw new CatalogDBException("VariableSet {id: " + variableSetId + "} does not exist");
            }
    */
    return endQuery("", startTime, studyQueryResult.first().getVariableSets());
}

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);//from   w  w  w  .j a v a  2  s  .co  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.ProjectMongoDBAdaptor.java

License:Apache License

@Override
public long getId(String userId, String projectAlias) throws CatalogDBException {
    QueryResult<Document> queryResult = userCollection.find(
            new BsonDocument("projects.alias", new BsonString(projectAlias)).append("id",
                    new BsonString(userId)),
            Projections.fields(Projections.include("projects.id"),
                    Projections.elemMatch("projects", Filters.eq("alias", projectAlias))),
            null);/*from   ww  w.j  a v a2s.co m*/
    /*
            QueryResult<DBObject> queryResult = userCollection.find(
        BasicDBObjectBuilder
                .start("projects.alias", projectAlias)
                .append("id", userId).get(),
        BasicDBObjectBuilder.start("projects.id", true)
                .append("projects", new BasicDBObject("$elemMatch", new BasicDBObject("alias", projectAlias))).get(),
        null
            );*/
    User user = parseUser(queryResult);
    if (user == null || user.getProjects().isEmpty()) {
        return -1;
    } else {
        return user.getProjects().get(0).getId();
    }
}

From source file:org.opencb.opencga.catalog.db.mongodb.SampleMongoDBAdaptor.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);/*from  w  w w .j  a  va 2 s.co 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);
        if (queryParam == null) {
            throw CatalogDBException.queryParamNotFound(key, "Samples");
        }
        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());
                    if (variableSetId > 0) {
                        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) {
            if (e instanceof CatalogDBException) {
                throw e;
            } else {
                throw new CatalogDBException("Error parsing query : " + query.toJson(), 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.UserMongoDBAdaptor.java

License:Apache License

@Override
public QueryResult<Session> getSession(String userId, String sessionId) throws CatalogDBException {
    long startTime = startQuery();

    //        BasicDBObject query = new BasicDBObject("id", userId);
    //        query.put("sessions.id", sessionId);
    Query query1 = new Query(QueryParams.ID.key(), userId).append(QueryParams.SESSION_ID.key(), sessionId);
    Bson bson = parseQuery(query1);/*from w  w w . j  a v  a2  s .  c o  m*/

    //        BasicDBObject projection = new BasicDBObject("sessions",
    //                new BasicDBObject("$elemMatch",
    //                        new BasicDBObject("id", sessionId)));
    Bson projection = Projections.elemMatch("sessions", Filters.eq("id", sessionId));

    //        QueryResult<DBObject> result = userCollection.find(query, projection, null);
    QueryResult<Document> documentQueryResult = userCollection.find(bson, projection, null);
    User user = parseUser(documentQueryResult);

    if (user != null) {
        return endQuery("getSession", startTime, user.getSessions());
    }

    return endQuery("getSession", startTime, Arrays.asList());
}