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

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

Introduction

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

Prototype

public static Bson exclude(final List<String> fieldNames) 

Source Link

Document

Creates a projection that excludes all of the given fields.

Usage

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

License:Apache License

public static Bson exclude(String... fields) {
    return Projections.exclude(fields);
}

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);/*from   w w w  .  j  a  v a2  s  .  c  om*/
    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;/*  www. ja  va  2s  .  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;
}