Java Code Examples for com.mongodb.client.model.Filters#in()

The following examples show how to use com.mongodb.client.model.Filters#in() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: MongoRepository.java    From javers with Apache License 2.0 5 votes vote down vote up
private Bson applyQueryParams(Bson query, Optional<QueryParams> queryParams) {
    if (queryParams.isPresent()) {
        QueryParams params = queryParams.get();

        if (params.from().isPresent()) {
            query = Filters.and(query, Filters.gte(COMMIT_DATE, UtilTypeCoreAdapters.serialize(params.from().get())));
        }
        if (params.to().isPresent()) {
            query =  Filters.and(query, Filters.lte(COMMIT_DATE, UtilTypeCoreAdapters.serialize(params.to().get())));
        }
        if (params.toCommitId().isPresent()) {
            BigDecimal commitId = params.toCommitId().get().valueAsNumber();
            query = Filters.and(query, Filters.lte(COMMIT_ID, commitId));
        }
        if (params.commitIds().size() > 0) {
            query = Filters.in(COMMIT_ID, params.commitIds().stream()
                    .map(CommitId::valueAsNumber).collect(Collectors.toSet()));
        }
        if (params.version().isPresent()) {
            query = Filters.and(query, createVersionQuery(params.version().get()));
        }
        if (params.author().isPresent()) {
            query = Filters.and(query, new BasicDBObject(COMMIT_AUTHOR, params.author().get()));
        }
        if (!params.commitProperties().isEmpty()) {
            query = addCommitPropertiesFilter(query, params.commitProperties());
        }
        if (params.changedProperty().isPresent()) {
            query = Filters.and(query, new BasicDBObject(CHANGED_PROPERTIES, params.changedProperty().get()));
        }
        if (params.snapshotType().isPresent()) {
            query = Filters.and(query, new BasicDBObject(SNAPSHOT_TYPE, params.snapshotType().get().name()));
        }

    }
    return query;
}
 
Example 2
Source File: CreateBsonPredicateVisitor.java    From ditto with Eclipse Public License 2.0 4 votes vote down vote up
@Override
public Function<String, Bson> visitIn(final List<?> values) {
    return fieldName -> Filters.in(fieldName, values);
}
 
Example 3
Source File: MongoDbTable.java    From beam with Apache License 2.0 4 votes vote down vote up
/**
 * Recursively translates a single RexNode to MongoDB Bson filter. Supports simple comparison
 * operations, negation, and nested conjunction/disjunction. Boolean fields are translated as an
 * `$eq` operation with a boolean `true`.
 *
 * @param node {@code RexNode} to translate.
 * @return {@code Bson} filter.
 */
private Bson translateRexNodeToBson(RexNode node) {
  final IntFunction<String> fieldIdToName = i -> getSchema().getField(i).getName();
  // Supported operations are described in MongoDbFilter#isSupported
  if (node instanceof RexCall) {
    RexCall compositeNode = (RexCall) node;
    List<RexLiteral> literals = new ArrayList<>();
    List<RexInputRef> inputRefs = new ArrayList<>();

    for (RexNode operand : compositeNode.getOperands()) {
      if (operand instanceof RexLiteral) {
        literals.add((RexLiteral) operand);
      } else if (operand instanceof RexInputRef) {
        inputRefs.add((RexInputRef) operand);
      }
    }

    // Operation is a comparison, since one of the operands in a field reference.
    if (inputRefs.size() == 1) {
      RexInputRef inputRef = inputRefs.get(0);
      String inputFieldName = fieldIdToName.apply(inputRef.getIndex());
      if (literals.size() > 0) {
        // Convert literal value to the same Java type as the field we are comparing to.
        Object literal = convertToExpectedType(inputRef, literals.get(0));

        switch (node.getKind()) {
          case IN:
            return Filters.in(inputFieldName, convertToExpectedType(inputRef, literals));
          case EQUALS:
            return Filters.eq(inputFieldName, literal);
          case NOT_EQUALS:
            return Filters.not(Filters.eq(inputFieldName, literal));
          case LESS_THAN:
            return Filters.lt(inputFieldName, literal);
          case GREATER_THAN:
            return Filters.gt(inputFieldName, literal);
          case GREATER_THAN_OR_EQUAL:
            return Filters.gte(inputFieldName, literal);
          case LESS_THAN_OR_EQUAL:
            return Filters.lte(inputFieldName, literal);
          default:
            // Encountered an unexpected node kind, RuntimeException below.
            break;
        }
      } else if (node.getKind().equals(SqlKind.NOT)) {
        // Ex: `where not boolean_field`
        return Filters.not(translateRexNodeToBson(inputRef));
      } else {
        throw new RuntimeException(
            "Cannot create a filter for an unsupported node: " + node.toString());
      }
    } else { // Operation is a conjunction/disjunction.
      switch (node.getKind()) {
        case AND:
          // Recursively construct filter for each operand of conjunction.
          return Filters.and(
              compositeNode.getOperands().stream()
                  .map(this::translateRexNodeToBson)
                  .collect(Collectors.toList()));
        case OR:
          // Recursively construct filter for each operand of disjunction.
          return Filters.or(
              compositeNode.getOperands().stream()
                  .map(this::translateRexNodeToBson)
                  .collect(Collectors.toList()));
        default:
          // Encountered an unexpected node kind, RuntimeException below.
          break;
      }
    }
    throw new RuntimeException(
        "Encountered an unexpected node kind: " + node.getKind().toString());
  } else if (node instanceof RexInputRef
      && node.getType().getSqlTypeName().equals(SqlTypeName.BOOLEAN)) {
    // Boolean field, must be true. Ex: `select * from table where bool_field`
    return Filters.eq(fieldIdToName.apply(((RexInputRef) node).getIndex()), true);
  }

  throw new RuntimeException(
      "Was expecting a RexCall or a boolean RexInputRef, but received: "
          + node.getClass().getSimpleName());
}
 
Example 4
Source File: FindBugsDAO.java    From repositoryminer with Apache License 2.0 4 votes vote down vote up
public Document findByClasses(String[] classes, String commit, Bson projection) {
	Bson clause1 = Filters.in("bugs.class", classes);
	Bson clause2 = new BasicDBObject("commit", commit);
	return findOne(Filters.and(clause1, clause2), projection);
}
 
Example 5
Source File: Mongos.java    From immutables with Apache License 2.0 4 votes vote down vote up
static Bson filterById(Iterable<?> ids) {
  return Filters.in(ID_FIELD_NAME, ids);
}
 
Example 6
Source File: AbstractFieldBsonCreator.java    From ditto with Eclipse Public License 2.0 2 votes vote down vote up
/**
 * Create filter BSON for global readability.
 *
 * @param authorizationSubjectIds authorization subject IDs for visibility restriction.
 * @return the BSON filter.
 */
public static Bson getGlobalReadBson(final Iterable<String> authorizationSubjectIds) {
    return Filters.in(FIELD_GLOBAL_READ, authorizationSubjectIds);
}