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

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

Introduction

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

Prototype

public static <TItem> Bson eq(final String fieldName, @Nullable final TItem value) 

Source Link

Document

Creates a filter that matches all documents where the value of the field name equals the specified value.

Usage

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

License:Apache License

private QueryResult<Individual> remove(int id, boolean force) throws CatalogDBException {
    long startTime = startQuery();
    checkId(id);//from   w w  w.jav a2  s.co m
    QueryResult<Individual> individual = get(id, new QueryOptions());
    Bson bson = Filters.eq(QueryParams.ID.key(), id);
    QueryResult<DeleteResult> remove = individualCollection.remove(bson, null);
    return endQuery("Delete individual", startTime, individual);
}

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

License:Apache License

public long getNewAutoIncrementId(String field) { //, MongoDBCollection metaCollection
    //        QueryResult<BasicDBObject> result = metaCollection.findAndModify(
    //                new BasicDBObject("_id", CatalogMongoDBAdaptor.METADATA_OBJECT_ID),  //Query
    //                new BasicDBObject(field, true),  //Fields
    //                null,
    //                new BasicDBObject("$inc", new BasicDBObject(field, 1)), //Update
    //                new QueryOptions("returnNew", true),
    //                BasicDBObject.class
    //        );/*w  ww.j av a  2  s  .c o m*/

    Bson query = Filters.eq(PRIVATE_ID, MongoDBAdaptorFactory.METADATA_OBJECT_ID);
    Document projection = new Document(field, true);
    Bson inc = Updates.inc(field, 1L);
    QueryOptions queryOptions = new QueryOptions("returnNew", true);
    QueryResult<Document> result = metaCollection.findAndUpdate(query, projection, null, inc, queryOptions);
    //        return (int) Float.parseFloat(result.getResult().get(0).get(field).toString());
    return result.getResult().get(0).getLong(field);
}

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

License:Apache License

@Override
public QueryResult<StudyAclEntry> getDaemonAcl(List<String> members) throws CatalogDBException {
    long startTime = startQuery();

    Bson match = Aggregates.match(Filters.eq(PRIVATE_ID, "METADATA"));
    Bson unwind = Aggregates.unwind("$" + CohortDBAdaptor.QueryParams.ACL.key());
    Bson match2 = Aggregates.match(Filters.in(CohortDBAdaptor.QueryParams.ACL_MEMBER.key(), members));
    Bson project = Aggregates.project(//from   ww w .ja  v a 2s.  c  om
            Projections.include(CohortDBAdaptor.QueryParams.ID.key(), CohortDBAdaptor.QueryParams.ACL.key()));

    QueryResult<Document> aggregate = metaCollection.aggregate(Arrays.asList(match, unwind, match2, project),
            null);
    StudyAclEntry result = null;
    if (aggregate.getNumResults() == 1) {
        result = parseObject(((Document) aggregate.getResult().get(0).get("acl")), StudyAclEntry.class);
    }
    return endQuery("get daemon Acl", startTime, Arrays.asList(result));
}

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

License:Apache License

@Deprecated
static long getNewAutoIncrementId(String field, MongoDBCollection metaCollection) {
    //        QueryResult<BasicDBObject> result = metaCollection.findAndModify(
    //                new BasicDBObject("_id", CatalogMongoDBAdaptor.METADATA_OBJECT_ID),  //Query
    //                new BasicDBObject(field, true),  //Fields
    //                null,
    //                new BasicDBObject("$inc", new BasicDBObject(field, 1)), //Update
    //                new QueryOptions("returnNew", true),
    //                BasicDBObject.class
    //        );//from  www  .j av  a  2  s .  co m

    Bson query = Filters.eq("_id", MongoDBAdaptorFactory.METADATA_OBJECT_ID);
    Document projection = new Document(field, true);
    Bson inc = Updates.inc(field, 1);
    QueryOptions queryOptions = new QueryOptions("returnNew", true);
    QueryResult<Document> result = metaCollection.findAndUpdate(query, projection, null, inc, queryOptions);
    //        return (int) Float.parseFloat(result.getResult().get(0).get(field).toString());
    return result.getResult().get(0).getInteger(field);
}

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 w w .  j a 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);
}

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;/* w ww.  ja  v  a2s  . com*/
    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))));
}

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

License:Apache License

@Override
public QueryResult<Project> insert(Project project, String userId, QueryOptions options)
        throws CatalogDBException {
    long startTime = startQuery();

    List<Study> studies = project.getStudies();
    if (studies == null) {
        studies = Collections.emptyList();
    }//from ww w.  j  a va 2 s.  c  om
    project.setStudies(Collections.<Study>emptyList());

    // Check if project.alias already exists.
    //        DBObject countQuery = BasicDBObjectBuilder
    //                .start("id", userId)
    //                .append("projects.alias", project.getAlias())
    //                .get();
    Bson countQuery = Filters.and(Filters.eq("id", userId), Filters.eq("projects.alias", project.getAlias()));
    QueryResult<Long> count = userCollection.count(countQuery);
    if (count.getResult().get(0) != 0) {
        throw new CatalogDBException(
                "Project {alias:\"" + project.getAlias() + "\"} already exists in this user");
    }
    //        if(getProjectId(userId, project.getAlias()) >= 0){
    //            throw new CatalogManagerException( "Project {alias:\"" + project.getAlias() + "\"} already exists in this user");
    //        }

    //Generate json
    //        int projectId = CatalogMongoDBUtils.getNewAutoIncrementId(metaCollection);
    long projectId = dbAdaptorFactory.getCatalogMetaDBAdaptor().getNewAutoIncrementId();
    project.setId(projectId);
    //        DBObject query = new BasicDBObject("id", userId);
    //        query.put("projects.alias", new BasicDBObject("$ne", project.getAlias()));
    Bson query = Filters.and(Filters.eq("id", userId), Filters.ne("projects.alias", project.getAlias()));

    Document projectDocument = projectConverter.convertToStorageType(project);
    //        DBObject update = new BasicDBObject("$push", new BasicDBObject("projects", projectDBObject));
    Bson update = Updates.push("projects", projectDocument);

    //Update object
    //        QueryResult<WriteResult> queryResult = userCollection.update(query, update, null);
    QueryResult<UpdateResult> queryResult = userCollection.update(query, update, null);

    if (queryResult.getResult().get(0).getModifiedCount() == 0) { // Check if the project has been inserted
        throw new CatalogDBException(
                "Project {alias:\"" + project.getAlias() + "\"} already exists in this user");
    }

    String errorMsg = "";
    for (Study study : studies) {
        String studyErrorMsg = dbAdaptorFactory.getCatalogStudyDBAdaptor()
                .insert(project.getId(), study, options).getErrorMsg();
        if (studyErrorMsg != null && !studyErrorMsg.isEmpty()) {
            errorMsg += ", " + study.getAlias() + ":" + studyErrorMsg;
        }
    }
    List<Project> result = get(project.getId(), null).getResult();
    return endQuery("Create Project", startTime, result, errorMsg, null);
}

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

License:Apache License

@Override
public QueryResult renameAlias(long projectId, String newProjectAlias) throws CatalogDBException {
    long startTime = startQuery();
    //        String projectOwner = getProjectOwner(projectId);
    ////  w w  w  .j av  a 2s  .  c o m
    //        int collisionProjectId = getProjectId(projectOwner, newProjectAlias);
    //        if (collisionProjectId != -1) {
    //            throw new CatalogManagerException("Couldn't rename project alias, alias already used in the same user");
    //        }

    QueryResult<Project> projectResult = get(projectId, null); // if projectId doesn't exist, an exception is raised
    Project project = projectResult.getResult().get(0);

    //String oldAlias = project.getAlias();
    project.setAlias(newProjectAlias);

    /*
    DBObject query = BasicDBObjectBuilder
        .start("projects.id", projectId)
        .append("projects.alias", new BasicDBObject("$ne", newProjectAlias))    // check that any other project in the user has
        // the new name
        .get();
    DBObject update = new BasicDBObject("$set",
        new BasicDBObject("projects.$.alias", newProjectAlias));
    */
    Bson query = Filters.and(Filters.eq("projects.id", projectId),
            Filters.ne("projects.alias", newProjectAlias));
    Bson update = Updates.set("projects.$.alias", newProjectAlias);

    QueryResult<UpdateResult> result = userCollection.update(query, update, null);
    if (result.getResult().get(0).getModifiedCount() == 0) { //Check if the the study has been inserted
        throw new CatalogDBException("Project {alias:\"" + newProjectAlias + "\"} already exists");
    }
    return endQuery("rename project alias", startTime, result);
}

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  . ja  va2 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.ProjectMongoDBAdaptor.java

License:Apache License

@Override
public String getOwnerId(long projectId) throws CatalogDBException {
    //        DBObject query = new BasicDBObject("projects.id", projectId);
    Bson query = Filters.eq("projects.id", projectId);

    //        DBObject projection = new BasicDBObject("id", "true");
    Bson projection = Projections.include("id");

    //        QueryResult<DBObject> result = userCollection.find(query, projection, null);
    QueryResult<Document> result = userCollection.find(query, projection, null);

    if (result.getResult().isEmpty()) {
        throw CatalogDBException.idNotFound("Project", projectId);
    } else {/*from   ww w.  jav  a  2  s .c  o m*/
        return result.getResult().get(0).get("id").toString();
    }
}