Example usage for com.mongodb.client FindIterable filter

List of usage examples for com.mongodb.client FindIterable filter

Introduction

In this page you can find the example usage for com.mongodb.client FindIterable filter.

Prototype

FindIterable<TResult> filter(@Nullable Bson filter);

Source Link

Document

Sets the query filter to apply to the query.

Usage

From source file:com.andersen.backendjaxrsproject.EmployeeServices.java

public String addEmployee(Employee employee) {
    String jsonString = "";
    try {/*from  w  w w.j a v  a  2s.c  o m*/
        ObjectMapper mapper = new ObjectMapper();

        MongoCollection<Document> employeeCollection = employeeDB.getCollection("EmployeeCollection");
        employee.setId(employeeCount + 1);

        jsonString = mapper.writeValueAsString(employee);
        Document doc = Document.parse(jsonString);
        employeeCollection.insertOne(doc);

        //get the document back from the database to verify that it was added
        Bson bson = (Bson) com.mongodb.util.JSON.parse("{'id': " + employee.getId() + "}");

        FindIterable<Document> find = employeeCollection.find();
        FindIterable<Document> filteredQuery = find.filter(bson);
        String newString = "";

        for (Document document : filteredQuery) {
            newString = newString + document.toJson() + "\n";
        }

        return newString;
    } catch (JsonProcessingException ex) {
        Logger.getLogger(EmployeeServices.class.getName()).log(Level.SEVERE, null, ex);
    }
    return jsonString;
}

From source file:com.andersen.backendjaxrsproject.EmployeeServices.java

public String getEmployeebyID(String id) {

    MongoCollection<Document> employeeCollection = employeeDB.getCollection("EmployeeCollection");
    Bson bson = (Bson) com.mongodb.util.JSON.parse("{'id': " + id + "}");

    FindIterable<Document> find = employeeCollection.find();
    FindIterable<Document> filteredQuery = find.filter(bson);
    String newString = "";
    for (Document document : filteredQuery) {
        newString = newString + document.toJson() + "\n";
    }//from w ww  . j av a  2s.co m
    return newString;
}

From source file:com.andersen.backendjaxrsproject.EmployeeServices.java

public String getEmployeebyLastName(String lastName) {

    MongoCollection<Document> employeeCollection = employeeDB.getCollection("EmployeeCollection");
    Bson bson = (Bson) com.mongodb.util.JSON.parse("{'lastName': '" + lastName + "'}");

    FindIterable<Document> find = employeeCollection.find();
    FindIterable<Document> filteredQuery = find.filter(bson);
    String newString = "";
    for (Document document : filteredQuery) {
        newString = newString + document.toJson() + "\n";
    }//from  www  . j a va 2  s .  c  o m
    return newString;
}

From source file:com.andersen.backendjaxrsproject.EmployeeServices.java

public String updateEmployee(long employeeID, String key, String updatedValue) {
    String newString = "";
    MongoCollection<Document> employeeCollection = employeeDB.getCollection("EmployeeCollection");
    ObjectMapper mapper = new ObjectMapper();

    Bson newValue = new Document(key, updatedValue);
    Bson filter = new Document("id", employeeID);
    Bson updateOperationDoc = new Document("$set", newValue);

    //Update the employee information
    UpdateResult result = employeeCollection.updateOne(filter, updateOperationDoc);

    //get the document back from the database to verify that it was added
    Bson bson = (Bson) com.mongodb.util.JSON.parse("{'id': " + employeeID + "}");

    FindIterable<Document> find = employeeCollection.find();
    FindIterable<Document> filteredQuery = find.filter(bson);
    for (Document document : filteredQuery) {
        newString = newString + document.toJson() + "\n";
    }/*from  ww w  .j  a v  a 2 s  . c  o m*/
    return newString;
}

From source file:com.redhat.thermostat.gateway.common.mongodb.executor.MongoExecutor.java

License:Open Source License

public MongoDataResultContainer execGetRequest(MongoCollection<Document> collection, Integer limit,
        Integer offset, String sort, List<String> queries, String includes, String excludes,
        Set<String> realms) {
    FindIterable<Document> documents = collection.find();
    MongoDataResultContainer queryDataContainer = new MongoDataResultContainer();

    Bson query = MongoRequestFilters.buildQuery(queries, realms);
    documents = documents.filter(query);
    long count = collection.count(query);
    queryDataContainer.setGetReqCount(count);
    queryDataContainer.setRemainingNumQueryDocuments((int) (count - (limit + offset)));
    documents = buildProjection(documents, includes, excludes);
    final Bson sortObject = MongoSortFilters.createSortObject(sort);
    documents = documents.sort(sortObject).limit(limit).skip(offset).batchSize(limit)
            .cursorType(CursorType.NonTailable);
    queryDataContainer.setQueryDataResult(documents);

    return queryDataContainer;
}

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 w ww.  j  av a 2s .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();
}