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

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

Introduction

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

Prototype

public static Bson excludeId() 

Source Link

Document

Creates a projection that excludes the _id field.

Usage

From source file:com.spleefleague.core.listeners.EnvironmentListener.java

@EventHandler(priority = EventPriority.HIGH)
public void rankCheck(AsyncPlayerPreLoginEvent e) {
    Document dbo = SpleefLeague.getInstance().getPluginDB().getCollection("Players")
            .find(new Document("uuid", e.getUniqueId().toString()))
            .projection(Projections.fields(Projections.include("rank"), Projections.excludeId())).first();
    if (dbo != null) {
        Rank rank = Rank.DEFAULT;/* w  ww .j  av a  2s.  c om*/
        try {
            rank = Rank.valueOf(dbo.getString("rank"));
        } catch (Exception ignored) {
        }
        if (rank == null || (!rank.hasPermission(SpleefLeague.getInstance().getMinimumJoinRank())
                && !SpleefLeague.getInstance().getExtraJoinRanks().contains(rank))) {
            e.setLoginResult(AsyncPlayerPreLoginEvent.Result.KICK_WHITELIST);
            e.setKickMessage(ChatColor.RED + "You are not of rank to join this server!");
        }
    }
}

From source file:module.ExportGeo.java

License:Open Source License

public ExportGeo() {

    String gseNumber = "GSE2109";

    // ===== Connection =====

    MongoClient mongoClient = MongoUtil.buildMongoClient();
    MongoDatabase db = mongoClient.getDatabase("epimed_experiments");

    MongoCollection<Document> collection = db.getCollection("samples");

    // ===== Find exp_group in the database =====

    List<Document> docExpGroup = collection.find(Filters.in("series", gseNumber))
            .projection(Projections.fields(Projections.include("exp_group"), Projections.excludeId()))
            .into(new ArrayList<Document>());

    List<Document> docParam = collection.find(Filters.in("series", gseNumber))
            .projection(Projections.fields(Projections.include("parameters"), Projections.excludeId()))
            .into(new ArrayList<Document>());

    mongoClient.close();//w ww .jav a2 s  .c o m

    // ===== Load Exp Group into a matrix =====

    List<String> headerExpGroup = new ArrayList<String>();
    List<Object> dataExpGroup = new ArrayList<Object>();

    for (int i = 0; i < docExpGroup.size(); i++) {
        Map<String, String> expGroup = (Map<String, String>) docExpGroup.get(i).get("exp_group");
        if (i == 0) {
            headerExpGroup.addAll(expGroup.keySet());
        }

        String[] dataLine = new String[headerExpGroup.size()];
        for (int j = 0; j < headerExpGroup.size(); j++) {
            dataLine[j] = expGroup.get(headerExpGroup.get(j));
        }
        dataExpGroup.add(dataLine);
    }

    // ===== Load Params into a matrix =====

    Set<String> headerParamSet = new HashSet<String>();
    List<String> headerParam = new ArrayList<String>();
    List<Object> dataParam = new ArrayList<Object>();

    for (int i = 0; i < docParam.size(); i++) {
        Map<String, String> param = (Map<String, String>) docParam.get(i).get("parameters");
        headerParamSet.addAll(param.keySet());
    }
    headerParam.addAll(headerParamSet);
    Collections.sort(headerParam);

    for (int i = 0; i < docParam.size(); i++) {
        Map<String, String> param = (Map<String, String>) docParam.get(i).get("parameters");
        String[] dataLine = new String[headerParam.size()];
        for (int j = 0; j < headerParam.size(); j++) {
            dataLine[j] = param.get(headerParam.get(j));
        }
        // System.out.println(Arrays.toString(dataLine));
        dataParam.add(dataLine);

    }

    // === Output ===

    String fileName = this.getOutputDirectory() + this.getDirSeparator() + "Export_Mongo_" + gseNumber + "_"
            + dateFormat.format(new Date()) + ".xlsx";
    System.out.println(fileName);
    XSSFWorkbook workbook = fileService.createWorkbook();
    fileService.addSheet(workbook, "exp_group" + dateFormat.format(new Date()), headerExpGroup, dataExpGroup);
    fileService.addSheet(workbook, "parameters_" + dateFormat.format(new Date()), headerParam, dataParam);
    fileService.writeWorkbook(workbook, fileName);

}

From source file:module.test.CustomExport.java

License:Open Source License

public CustomExport() {

    // ===== Connection =====

    MongoClient mongoClient = MongoUtil.buildMongoClient();
    MongoDatabase db = mongoClient.getDatabase("epimed_experiments");

    MongoCollection<Document> collection = db.getCollection("samples");

    // ===== Find exp_group in the database =====

    // === Query 1 ===
    /*/*ww  w. ja v a 2 s  .c o  m*/
    String queryName = "breast_cancer_GPL570";
    List<Bson> filters = new ArrayList<Bson>();
    filters.add(Filters.eq("exp_group.id_platform", "GPL570"));
    filters.add(Filters.eq("exp_group.id_topology_group", "C50"));
    filters.add(Filters.eq("exp_group.id_tissue_status", 3)); // tumoral
     */

    // === Query 2 ===
    /*
    String queryName = "breast_normal_GPL570";
    List<Bson> filters = new ArrayList<Bson>();
    filters.add(Filters.eq("exp_group.id_platform", "GPL570"));
    filters.add(Filters.eq("exp_group.id_topology_group", "C50"));
    filters.add(Filters.eq("exp_group.id_tissue_status", 1)); // normal
    */

    // === Query 3 ===
    String queryName = "breast_cancer_with_survival_GPL570";
    List<Bson> filters = new ArrayList<Bson>();
    filters.add(Filters.eq("exp_group.id_platform", "GPL570"));
    filters.add(Filters.eq("exp_group.id_topology_group", "C50"));
    filters.add(Filters.eq("exp_group.id_tissue_status", 3)); // tumoral
    filters.add(Filters.or(Filters.ne("exp_group.os_months", null), Filters.ne("exp_group.dfss_months", null),
            Filters.ne("exp_group.relapsed", null), Filters.ne("exp_group.dead", null)));

    Bson filter = Filters.and(filters);
    Long nbSamples = collection.count(filter);
    List<String> listSeries = collection.distinct("exp_group.main_gse_number", filter, String.class)
            .into(new ArrayList<String>());
    queryName = queryName + "_" + nbSamples + "_samples_" + listSeries.size() + "_series";

    List<Document> docExpGroup = collection.find(filter)
            .projection(Projections.fields(Projections.include("exp_group"), Projections.excludeId()))
            .into(new ArrayList<Document>());

    List<Document> docParam = collection.find(filter)
            .projection(Projections.fields(Projections.include("parameters"), Projections.excludeId()))
            .into(new ArrayList<Document>());

    mongoClient.close();

    // ===== Load Exp Group into a matrix =====

    List<String> headerExpGroup = new ArrayList<String>();
    List<Object> dataExpGroup = new ArrayList<Object>();

    for (int i = 0; i < docExpGroup.size(); i++) {
        Map<String, String> expGroup = (Map<String, String>) docExpGroup.get(i).get("exp_group");
        if (i == 0) {
            headerExpGroup.addAll(expGroup.keySet());
        }

        Object[] dataLine = new Object[headerExpGroup.size()];
        for (int j = 0; j < headerExpGroup.size(); j++) {
            dataLine[j] = expGroup.get(headerExpGroup.get(j));
        }
        dataExpGroup.add(dataLine);
    }

    // ===== Load Params into a matrix =====

    Set<String> headerParamSet = new HashSet<String>();
    List<String> headerParam = new ArrayList<String>();
    List<Object> dataParam = new ArrayList<Object>();

    for (int i = 0; i < docParam.size(); i++) {
        Map<String, String> param = (Map<String, String>) docParam.get(i).get("parameters");
        headerParamSet.addAll(param.keySet());
    }
    headerParam.addAll(headerParamSet);
    Collections.sort(headerParam);

    for (int i = 0; i < docParam.size(); i++) {
        Map<String, String> param = (Map<String, String>) docParam.get(i).get("parameters");
        Object[] dataLine = new Object[headerParam.size()];
        for (int j = 0; j < headerParam.size(); j++) {
            dataLine[j] = param.get(headerParam.get(j));
        }
        // System.out.println(Arrays.toString(dataLine));
        dataParam.add(dataLine);

    }

    // === Output ===

    String fileName = this.getOutputDirectory() + this.getDirSeparator() + "EpiMed_database_" + queryName + "_"
            + dateFormat.format(new Date()) + ".xlsx";
    System.out.println(fileName);
    XSSFWorkbook workbook = fileService.createWorkbook();
    fileService.addSheet(workbook, "exp_group_" + dateFormat.format(new Date()), headerExpGroup, dataExpGroup);
    fileService.addSheet(workbook, "parameters_" + dateFormat.format(new Date()), headerParam, dataParam);
    fileService.writeWorkbook(workbook, fileName);

}

From source file:org.opencb.cellbase.lib.impl.GeneMongoDBAdaptor.java

License:Apache License

@Override
public QueryResult getTfbs(Query query, QueryOptions queryOptions) {
    Bson bsonQuery = parseQuery(query);//  w  w w .  j av a  2 s  .  c  o  m
    Bson match = Aggregates.match(bsonQuery);

    // We parse user's exclude options, ONLY _id can be added if exists
    Bson includeAndExclude;
    Bson exclude = null;
    if (queryOptions != null && queryOptions.containsKey("exclude")) {
        List<String> stringList = queryOptions.getAsStringList("exclude");
        if (stringList.contains("_id")) {
            exclude = Aggregates.project(Projections.exclude("_id"));
        }
    }
    if (exclude != null) {
        includeAndExclude = Aggregates
                .project(Projections.fields(Projections.excludeId(), Projections.include("transcripts.tfbs")));
    } else {
        includeAndExclude = Aggregates.project(Projections.include("transcripts.tfbs"));
    }

    Bson unwind = Aggregates.unwind("$transcripts");
    Bson unwind2 = Aggregates.unwind("$transcripts.tfbs");

    // This project the three fields of Xref to the top of the object
    Document document = new Document("tfName", "$transcripts.tfbs.tfName");
    document.put("pwm", "$transcripts.tfbs.pwm");
    document.put("chromosome", "$transcripts.tfbs.chromosome");
    document.put("start", "$transcripts.tfbs.start");
    document.put("end", "$transcripts.tfbs.end");
    document.put("strand", "$transcripts.tfbs.strand");
    document.put("relativeStart", "$transcripts.tfbs.relativeStart");
    document.put("relativeEnd", "$transcripts.tfbs.relativeEnd");
    document.put("score", "$transcripts.tfbs.score");
    Bson project = Aggregates.project(document);

    return mongoDBCollection.aggregate(Arrays.asList(match, includeAndExclude, unwind, unwind2, project),
            queryOptions);
}

From source file:org.opencb.commons.datastore.mongodb.MongoDBQueryUtils.java

License:Apache License

protected static Bson getProjection(Bson projection, QueryOptions options) {
    Bson projectionResult = null;/*from w  w  w  . ja va 2 s . c o  m*/
    List<Bson> projections = new ArrayList<>();

    // It is too risky to merge projections, if projection alrady exists we return it as it is, otherwise we create a new one.
    if (projection != null) {
        //            projections.add(projection);
        return projection;
    }

    if (options != null) {
        // Select which fields are excluded and included in the query
        // Read and process 'include'/'exclude'/'elemMatch' field from 'options' object

        Bson include = null;
        if (options.containsKey(QueryOptions.INCLUDE)) {
            Object includeObject = options.get(QueryOptions.INCLUDE);
            if (includeObject != null) {
                if (includeObject instanceof Bson) {
                    include = (Bson) includeObject;
                } else {
                    List<String> includeStringList = options.getAsStringList(QueryOptions.INCLUDE, ",");
                    if (includeStringList != null && includeStringList.size() > 0) {
                        include = Projections.include(includeStringList);
                    }
                }
            }
        }

        Bson exclude = null;
        boolean excludeId = false;
        if (options.containsKey(QueryOptions.EXCLUDE)) {
            Object excludeObject = options.get(QueryOptions.EXCLUDE);
            if (excludeObject != null) {
                if (excludeObject instanceof Bson) {
                    exclude = (Bson) excludeObject;
                } else {
                    List<String> excludeStringList = options.getAsStringList(QueryOptions.EXCLUDE, ",");
                    if (excludeStringList != null && excludeStringList.size() > 0) {
                        exclude = Projections.exclude(excludeStringList);
                        excludeId = excludeStringList.contains("_id");
                    }
                }
            }
        }

        // If both include and exclude exist we only add include
        if (include != null) {
            projections.add(include);
            // MongoDB allows to exclude _id when include is present
            if (excludeId) {
                projections.add(Projections.excludeId());
            }
        } else {
            if (exclude != null) {
                projections.add(exclude);
            }
        }

        if (options.containsKey(MongoDBCollection.ELEM_MATCH)) {
            Object elemMatch = options.get(MongoDBCollection.ELEM_MATCH);
            if (elemMatch != null && elemMatch instanceof Bson) {
                projections.add((Bson) elemMatch);
            }
        }

        //            List<String> includeStringList = options.getAsStringList(MongoDBCollection.INCLUDE, ",");
        //            if (includeStringList != null && includeStringList.size() > 0) {
        //                projections.add(Projections.include(includeStringList));
        ////                for (Object field : includeStringList) {
        ////                    projection.put(field.toString(), 1);
        ////                }
        //            } else {
        //                List<String> excludeStringList = options.getAsStringList(MongoDBCollection.EXCLUDE, ",");
        //                if (excludeStringList != null && excludeStringList.size() > 0) {
        //                    projections.add(Projections.exclude(excludeStringList));
        ////                    for (Object field : excludeStringList) {
        ////                        projection.put(field.toString(), 0);
        ////                    }
        //                }
        //            }
    }

    if (projections.size() > 0) {
        projectionResult = Projections.fields(projections);
    }

    return projectionResult;
}

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

License:Apache License

@Override
public QueryResult<AclEntry> getProjectAcl(long projectId, String userId) throws CatalogDBException {
    long startTime = startQuery();
    /*//  w  w w  .  ja v a2 s  .co  m
    DBObject match1 = new BasicDBObject("$match", new BasicDBObject("projects.id", projectId));
    DBObject project = new BasicDBObject("$project", BasicDBObjectBuilder
        .start("_id", false)
        .append("projects.acl", true)
        .append("projects.id", true).get());
    DBObject unwind1 = new BasicDBObject("$unwind", "$projects");
    DBObject match2 = new BasicDBObject("$match", new BasicDBObject("projects.id", projectId));
    DBObject unwind2 = new BasicDBObject("$unwind", "$projects.acl");
    DBObject match3 = new BasicDBObject("$match", new BasicDBObject("projects.acl.userId", userId));
            
    */
    Bson match1 = Aggregates.match(Filters.eq("projects.id", projectId));
    //        Bson project = Projections.fields(Projections.excludeId(), Projections.include("projects.acl", "projects.id"));
    Bson project = Aggregates.project(
            Projections.fields(Projections.excludeId(), Projections.include("projects.acl", "projects.id")));
    Bson unwind1 = Aggregates.unwind("$projects");
    Bson match2 = Aggregates.match(Filters.eq("projects.id", projectId));
    Bson unwind2 = Aggregates.unwind("$projects.acl");
    Bson match3 = Aggregates.match(Filters.eq("projects.acl.userId", userId));

    List<Bson> operations = new LinkedList<>();
    operations.add(match1);
    operations.add(project);
    operations.add(unwind1);
    operations.add(match2);
    operations.add(unwind2);
    operations.add(match3);

    QueryResult aggregate = userCollection.aggregate(operations, null);
    List<AclEntry> acls = new LinkedList<>();
    if (aggregate.getNumResults() != 0) {
        //            DBObject aclObject = (DBObject) ((DBObject) ((DBObject) aggregate.getResult().get(0)).get("projects")).get("acl");
        Document aclObject = (Document) ((Document) ((Document) aggregate.getResult().get(0)).get("projects"))
                .get("acl");
        AclEntry acl = parseObject(aclObject, AclEntry.class);
        acls.add(acl);
    }
    return endQuery("get project ACL", startTime, acls);
}