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

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

Introduction

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

Prototype

public static Bson and(final Bson... filters) 

Source Link

Document

Creates a filter that performs a logical AND of the provided list of filters.

Usage

From source file:org.apache.rya.mongodb.aggregation.AggregationPipelineQueryNode.java

License:Apache License

/**
 * Add a join with an individual {@link StatementPattern} to the pipeline.
 * @param sp The statement pattern to join with
 * @return true if the join was successfully added to the pipeline.
 */// w ww . j  av a2 s.co m
public boolean joinWith(final StatementPattern sp) {
    Preconditions.checkNotNull(sp);
    // 1. Determine shared variables and new variables
    final StatementVarMapping spMap = new StatementVarMapping(sp, varToOriginalName);
    final NavigableSet<String> sharedVars = new ConcurrentSkipListSet<>(spMap.varNames());
    sharedVars.retainAll(assuredBindingNames);
    // 2. Join on one shared variable
    final String joinKey = sharedVars.pollFirst();
    final String collectionName = collection.getNamespace().getCollectionName();
    Bson join;
    if (joinKey == null) {
        return false;
    } else {
        join = Aggregates.lookup(collectionName, HASHES + "." + joinKey, spMap.hashField(joinKey),
                JOINED_TRIPLE);
    }
    pipeline.add(join);
    // 3. Unwind the joined triples so each document represents a binding
    //   set (solution) from the base branch and a triple that may match.
    pipeline.add(Aggregates.unwind("$" + JOINED_TRIPLE));
    // 4. (Optional) If there are any shared variables that weren't used as
    //   the join key, project all existing fields plus a new field that
    //   tests the equality of those shared variables.
    final BasicDBObject matchOpts = getMatchExpression(sp, JOINED_TRIPLE);
    if (!sharedVars.isEmpty()) {
        final List<Bson> eqTests = new LinkedList<>();
        for (final String varName : sharedVars) {
            final String oldField = valueFieldExpr(varName);
            final String newField = joinFieldExpr(spMap.valueField(varName));
            final Bson eqTest = new Document("$eq", Arrays.asList(oldField, newField));
            eqTests.add(eqTest);
        }
        final Bson eqProjectOpts = Projections.fields(Projections.computed(FIELDS_MATCH, Filters.and(eqTests)),
                Projections.include(JOINED_TRIPLE, VALUES, HASHES, TYPES, LEVEL, TIMESTAMP));
        pipeline.add(Aggregates.project(eqProjectOpts));
        matchOpts.put(FIELDS_MATCH, true);
    }
    // 5. Filter for solutions whose triples match the joined statement
    //  pattern, and, if applicable, whose additional shared variables
    //  match the current solution.
    pipeline.add(Aggregates.match(matchOpts));
    // 6. Project the results to include variables from the new SP (with
    // appropriate renaming) and variables referenced only in the base
    // pipeline (with previous names).
    final Bson finalProjectOpts = new StatementVarMapping(sp, varToOriginalName)
            .getProjectExpression(assuredBindingNames, str -> joinFieldExpr(str));
    assuredBindingNames.addAll(spMap.varNames());
    bindingNames.addAll(spMap.varNames());
    pipeline.add(Aggregates.project(finalProjectOpts));
    return true;
}

From source file:org.eclipse.ditto.services.thingsearch.persistence.read.criteria.visitors.CreateBsonVisitor.java

License:Open Source License

@Override
public Bson visitAnd(final Stream<Bson> conjuncts) {
    return Filters.and(conjuncts.collect(Collectors.toList()));
}

From source file:org.eclipse.ditto.services.thingsearch.persistence.write.model.AbstractWriteModel.java

License:Open Source License

/**
 * Get the filter of this write model./*w  ww.jav  a  2 s . co m*/
 *
 * @return filter on search index documents.
 */
public Bson getFilter() {
    return Filters.and(Filters.eq(FIELD_ID, new BsonString(metadata.getThingId().toString())));
}

From source file:org.hibernate.ogm.datastore.mongodb.binarystorage.GridFSStorageManager.java

License:LGPL

private void deleteExistingContent(String fieldName, Object documentId, GridFSBucket gridFSFilesBucket) {
    GridFSFindIterable results = gridFSFilesBucket
            .find(Filters.and(Filters.eq("filename", fileName(fieldName, documentId))));
    try (MongoCursor<GridFSFile> iterator = results.iterator()) {
        while (iterator.hasNext()) {
            GridFSFile next = iterator.next();
            gridFSFilesBucket.delete(next.getId());
        }/*from   w  w  w .ja  v a  2  s  .c  o m*/
    }
}

From source file:org.jnosql.diana.mongodb.document.DocumentQueryConversor.java

License:Open Source License

public static Bson convert(DocumentCondition condition) {
    Document document = condition.getDocument();
    Object value = document.getValue().get();
    switch (condition.getCondition()) {
    case EQUALS://w  ww  .  jav  a  2 s .c  o m
        return Filters.eq(document.getName(), value);
    case GREATER_THAN:
        return Filters.gt(document.getName(), value);
    case GREATER_EQUALS_THAN:
        return Filters.gte(document.getName(), value);
    case LESSER_THAN:
        return Filters.lt(document.getName(), value);
    case LESSER_EQUALS_THAN:
        return Filters.lte(document.getName(), value);
    case IN:
        return Filters.in(document.getName(), value);
    case LIKE:
        return Filters.regex(document.getName(), value.toString());
    case AND:
        List<Document> andList = condition.getDocument().getValue().get(new TypeReference<List<Document>>() {
        });
        return Filters.and(andList.stream().map(d -> new BasicDBObject(d.getName(), d.getValue().get()))
                .toArray(BasicDBObject[]::new));
    case OR:
        List<Document> orList = condition.getDocument().getValue().get(new TypeReference<List<Document>>() {
        });
        return Filters.or(orList.stream().map(d -> new BasicDBObject(d.getName(), d.getValue().get()))
                .toArray(BasicDBObject[]::new));
    default:
        throw new UnsupportedOperationException(
                "The condition " + condition.getCondition() + " is not supported from mongoDB diana driver");
    }
}

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

License:Apache License

private Bson parseQuery(Query query) {
    logger.debug("Parsing query...");
    Bson filtersBson = null;/*  w  ww . j av  a 2  s . c o  m*/

    // No filtering parameters mean all records
    if (query.size() > 0) {
        Bson commonFiltersBson = getCommonFilters(query);
        Set<String> sourceContent = query.getAsStringList(QueryParams.SOURCE.key()) != null
                ? new HashSet<>(query.getAsStringList(QueryParams.SOURCE.key()))
                : null;
        List<Bson> sourceSpecificFilterList = new ArrayList<>();
        getClinvarFilters(query, sourceContent, sourceSpecificFilterList);
        getCosmicFilters(query, sourceContent, sourceSpecificFilterList);
        getGwasFilters(query, sourceContent, sourceSpecificFilterList);

        if (sourceSpecificFilterList.size() > 0 && commonFiltersBson != null) {
            List<Bson> filtersBsonList = new ArrayList<>();
            filtersBsonList.add(commonFiltersBson);
            filtersBsonList.add(Filters.or(sourceSpecificFilterList));
            filtersBson = Filters.and(filtersBsonList);
        } else if (commonFiltersBson != null) {
            filtersBson = commonFiltersBson;
        } else if (sourceSpecificFilterList.size() > 0) {
            filtersBson = Filters.or(sourceSpecificFilterList);
        }
    }

    if (filtersBson != null) {
        return filtersBson;
    } else {
        return new Document();
    }
}

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

License:Apache License

private void getCosmicFilters(Query query, Set<String> sourceContent, List<Bson> sourceBson) {
    // If only clinvar-specific filters are provided it must be avoided to include the source=cosmic condition since
    // sourceBson is going to be an OR list
    List<Bson> andBson = new ArrayList<>();
    if (!(query.containsKey(QueryParams.CLINVARRCV.key()) || query.containsKey(QueryParams.CLINVARCLINSIG.key())
            || query.containsKey(QueryParams.CLINVARREVIEW.key())
            || query.containsKey(QueryParams.CLINVARTYPE.key())
            || query.containsKey(QueryParams.CLINVARRS.key()))) {
        if (sourceContent != null && sourceContent.contains("cosmic")) {
            andBson.add(Filters.eq("source", "cosmic"));
        }//ww  w . j  ava 2  s .co m

        createOrQuery(query, QueryParams.COSMICID.key(), "mutationID", andBson);
        if (andBson.size() == 1) {
            sourceBson.add(andBson.get(0));
        } else if (andBson.size() > 0) {
            sourceBson.add(Filters.and(andBson));
        }
    }
}

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

License:Apache License

private void getClinvarFilters(Query query, Set<String> sourceContent, List<Bson> sourceBson) {
    List<Bson> andBsonList = new ArrayList<>();

    if (sourceContent != null && sourceContent.contains("clinvar")) {
        andBsonList.add(Filters.eq("source", "clinvar"));
    }/*from ww w. j ava 2  s . c o m*/

    createOrQuery(query, QueryParams.CLINVARRCV.key(),
            "clinvarSet.referenceClinVarAssertion.clinVarAccession.acc", andBsonList);
    createClinvarRsQuery(query, andBsonList);
    createClinvarTypeQuery(query, andBsonList);
    createClinvarReviewQuery(query, andBsonList);
    createClinvarClinicalSignificanceQuery(query, andBsonList);

    if (andBsonList.size() == 1) {
        sourceBson.add(andBsonList.get(0));
    } else if (andBsonList.size() > 0) {
        sourceBson.add(Filters.and(andBsonList));
    }
}

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

License:Apache License

private void createClinvarRsQuery(Query query, List<Bson> andBsonList) {
    if (query != null && query.getString(QueryParams.CLINVARRS.key()) != null
            && !query.getString(QueryParams.CLINVARRS.key()).isEmpty()) {
        List<String> queryList = query.getAsStringList(QueryParams.CLINVARRS.key());
        if (queryList.size() == 1) {
            andBsonList.add(Filters.eq("clinvarSet.referenceClinVarAssertion.measureSet.measure.xref.id",
                    queryList.get(0).substring(2)));
            andBsonList//from w w  w.j a  v a  2  s. c  om
                    .add(Filters.eq("clinvarSet.referenceClinVarAssertion.measureSet.measure.xref.type", "rs"));
        } else {
            List<Bson> orBsonList = new ArrayList<>(queryList.size());
            for (String queryItem : queryList) {
                List<Bson> innerAndBsonList = new ArrayList<>();
                innerAndBsonList
                        .add(Filters.eq("clinvarSet.referenceClinVarAssertion.measureSet.measure.xref.id",
                                queryList.get(0).substring(2)));
                innerAndBsonList.add(
                        Filters.eq("clinvarSet.referenceClinVarAssertion.measureSet.measure.xref.type", "rs"));
                orBsonList.add(Filters.and(innerAndBsonList));
            }
            andBsonList.add(Filters.or(orBsonList));
        }
    }
}

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

License:Apache License

private Bson getCommonFilters(Query query) {
    List<Bson> andBsonList = new ArrayList<>();
    createRegionQuery(query, QueryParams.REGION.key(), andBsonList);

    createOrQuery(query, QueryParams.SO.key(), "annot.consequenceTypes.sequenceOntologyTerms.name",
            andBsonList);/*from w  w  w .  j ava 2s  .c  om*/
    createOrQuery(query, QueryParams.GENE.key(), "_geneIds", andBsonList);
    createPhenotypeQuery(query, andBsonList);

    if (andBsonList.size() == 1) {
        return andBsonList.get(0);
    } else if (andBsonList.size() > 1) {
        return Filters.and(andBsonList);
    } else {
        return null;
    }
}