List of usage examples for com.mongodb.client.model Filters or
public static Bson or(final Bson... filters)
From source file:com.px100systems.data.plugin.storage.mongo.FilterQueryBuilder.java
License:Open Source License
@Override public Bson convert(or predicate) { List<Bson> q = new ArrayList<>(); for (Criteria c : predicate.getMembers()) q.add(c.convert(this)); return Filters.or(q); }
From source file:com.streamsets.pipeline.stage.origin.mongodb.oplog.MongoDBOplogSource.java
License:Apache License
private void prepareCursor(int timestampSeconds, int ordinal, List<OplogOpType> filterOplogTypes, int batchSize) { LOG.debug("Getting new cursor with offset - TimeStampInSeconds:'{}', Ordinal : '{}' and Batch Size : '{}'", timestampSeconds, ordinal, batchSize); FindIterable<Document> mongoCursorIterable = mongoCollection.find() //As the collection is a capped collection we use Tailable cursor which will return results in natural order in this case //based on ts timestamp field. //Tailable Await does not return and blocks, so we are using tailable. .cursorType(CursorType.Tailable).batchSize(batchSize); List<Bson> andFilters = new ArrayList<>(); //Only filter if we already have saved/initial offset specified or else both time_t and ordinal will not be -1. if (timestampSeconds > 0 && ordinal >= 0) { andFilters.add(Filters.gt(TIMESTAMP_FIELD, new BsonTimestamp(timestampSeconds, ordinal))); }//from www.j a v a 2 s . c o m if (!filterOplogTypes.isEmpty()) { List<Bson> oplogOptypeFilters = new ArrayList<>(); Set<OplogOpType> oplogOpTypesSet = new HashSet<>(); for (OplogOpType filterOplogopType : filterOplogTypes) { if (oplogOpTypesSet.add(filterOplogopType)) { oplogOptypeFilters.add(Filters.eq(OP_TYPE_FIELD, filterOplogopType.getOp())); } } //Add an or filter for filtered Or Types andFilters.add(Filters.or(oplogOptypeFilters)); } //Finally and timestamp with oplog filters if (!andFilters.isEmpty()) { mongoCursorIterable = mongoCursorIterable.filter(Filters.and(andFilters)); } cursor = mongoCursorIterable.iterator(); }
From source file:io.lumeer.storage.mongodb.dao.collection.MongoDataDao.java
License:Open Source License
private Bson createFilter(String collectionId, SearchQuery query) { List<Bson> filters = new ArrayList<>(); // does not work as expected - cannot search for a single character for example, only whole words if (query.isFulltextQuery()) { Collection collection = collectionDao.getCollectionById(collectionId); List<Attribute> fulltextAttrs = collection.getAttributes().stream() .filter(attr -> attr.getName().toLowerCase().contains(query.getFulltext().toLowerCase())) .collect(Collectors.toList()); // we only search by presence of the matching attributes if (fulltextAttrs.size() > 0) { filters.add(Filters.or(fulltextAttrs.stream().map(attr -> Filters.exists(attr.getId())) .collect(Collectors.toList()))); } else if (collection.getAttributes().size() > 0) { // we search by content filters.add(Filters.or(collection.getAttributes().stream() .map(attr -> Filters.regex(attr.getId(), query.getFulltext(), "i")) .collect(Collectors.toList()))); }/*from w w w. j a v a2 s . c o m*/ } if (query.isDocumentIdsQuery()) { List<ObjectId> ids = query.getDocumentIds().stream().filter(ObjectId::isValid).map(ObjectId::new) .collect(Collectors.toList()); if (!ids.isEmpty()) { filters.add(Filters.in(ID, ids)); } } if (query.isFiltersQuery()) { List<Bson> attributeFilters = query.getFilters().stream().map(this::attributeFilter) .filter(Objects::nonNull).collect(Collectors.toList()); filters.addAll(attributeFilters); } final Bson result = filters.size() > 0 ? Filters.and(filters) : new Document(); return result; }
From source file:io.lumeer.storage.mongodb.util.MongoFilters.java
License:Open Source License
public static Bson permissionsFilter(DatabaseQuery databaseQuery) { final List<Bson> filters = databaseQuery.getGroups().stream().map(MongoFilters::groupPermissionsFilter) .collect(Collectors.toList()); filters.addAll(databaseQuery.getUsers().stream().map(MongoFilters::userPermissionsFilter) .collect(Collectors.toList())); return Filters.or(filters); }
From source file:mongodb.clients.percunia.mongo.Restriction.java
License:Apache License
public static Bson or(Bson... restrictions) { return Filters.or(restrictions); }
From source file:org.eclipse.ditto.services.thingsearch.persistence.read.criteria.visitors.CreateBsonVisitor.java
License:Open Source License
@Override public Bson visitOr(final Stream<Bson> disjoints) { return Filters.or(disjoints.collect(Collectors.toList())); }
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:/*from w w w . j a v a 2 s .com*/ 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;//from ww w. ja v a 2 s . co 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 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 ava2s.co m*/ .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.MongoDBAdaptor.java
License:Apache License
protected void createRegionQuery(Query query, String queryParam, List<Bson> andBsonList) { if (query != null && query.getString(queryParam) != null && !query.getString(queryParam).isEmpty()) { List<Region> regions = Region.parseRegions(query.getString(queryParam)); if (regions != null && regions.size() > 0) { // if there is only one region we add the AND filter directly to the andBsonList passed if (regions.size() == 1) { Bson chromosome = Filters.eq("chromosome", regions.get(0).getChromosome()); Bson start = Filters.lte("start", regions.get(0).getEnd()); Bson end = Filters.gte("end", regions.get(0).getStart()); andBsonList.add(Filters.and(chromosome, start, end)); } else { // when multiple regions then we create and OR list before add it to andBsonList List<Bson> orRegionBsonList = new ArrayList<>(regions.size()); for (Region region : regions) { Bson chromosome = Filters.eq("chromosome", region.getChromosome()); Bson start = Filters.lte("start", region.getEnd()); Bson end = Filters.gte("end", region.getStart()); orRegionBsonList.add(Filters.and(chromosome, start, end)); }//from w w w.j a v a2 s . c om andBsonList.add(Filters.or(orRegionBsonList)); } } else { logger.warn("Region query no created, region object is null or empty."); } } }