Example usage for com.mongodb.client MongoCollection withReadConcern

List of usage examples for com.mongodb.client MongoCollection withReadConcern

Introduction

In this page you can find the example usage for com.mongodb.client MongoCollection withReadConcern.

Prototype

MongoCollection<TDocument> withReadConcern(ReadConcern readConcern);

Source Link

Document

Create a new MongoCollection instance with a different read concern.

Usage

From source file:joliex.mongodb.MongoDbConnector.java

@RequestResponse
public Value find(Value request) throws FaultException {
    Value v = Value.create();//from ww w.ja  v  a2  s.c o m
    FindIterable<BsonDocument> iterable = null;
    ;
    try {

        String collectionName = request.getFirstChild("collection").strValue();
        MongoCollection<BsonDocument> collection = db.getCollection(collectionName, BsonDocument.class);
        if (request.hasChildren("readConcern")) {

            ReadConcern readConcern = new ReadConcern(
                    ReadConcernLevel.fromString(request.getFirstChild("readConcern").strValue()));
            collection.withReadConcern(readConcern);
        }

        if (request.hasChildren("filter")) {
            BsonDocument bsonQueryDocument = BsonDocument.parse(request.getFirstChild("filter").strValue());
            prepareBsonQueryData(bsonQueryDocument, request.getFirstChild("filter"));
            printlnJson("Query filter", bsonQueryDocument);
            if (request.hasChildren("sort") && request.hasChildren("limit")) {
                BsonDocument bsonSortDocument = BsonDocument.parse(request.getFirstChild("sort").strValue());
                prepareBsonQueryData(bsonSortDocument, request.getFirstChild("sort"));
                printlnJson("Query sort", bsonSortDocument);
                int limitQuery = request.getFirstChild("limit").intValue();
                iterable = collection.find(bsonQueryDocument).sort(bsonSortDocument).limit(limitQuery);
            }

            if (request.hasChildren("sort") && request.hasChildren("limit") && request.hasChildren("skip")) {
                BsonDocument bsonSortDocument = BsonDocument.parse(request.getFirstChild("sort").strValue());
                prepareBsonQueryData(bsonSortDocument, request.getFirstChild("sort"));
                printlnJson("Query sort", bsonSortDocument);
                int limitQuery = request.getFirstChild("limit").intValue();
                int skipPosition = request.getFirstChild("skip").intValue();
                iterable = collection.find(bsonQueryDocument).sort(bsonSortDocument).limit(limitQuery)
                        .skip(skipPosition);
            }

            if (request.hasChildren("sort") && !request.hasChildren("limit")) {
                BsonDocument bsonSortDocument = BsonDocument.parse(request.getFirstChild("sort").strValue());
                prepareBsonQueryData(bsonSortDocument, request.getFirstChild("sort"));
                printlnJson("Query sort", bsonSortDocument);
                iterable = collection.find(bsonQueryDocument).sort(bsonSortDocument);
            }
            if (!request.hasChildren("sort") && request.hasChildren("limit")) {
                int limitQuery = request.getFirstChild("limit").intValue();
                iterable = collection.find(bsonQueryDocument).limit(limitQuery);

            }
            if (!request.hasChildren("sort") && !request.hasChildren("limit")) {

                iterable = collection.find(bsonQueryDocument);
            }

        } else {

            if (request.hasChildren("sort") && request.hasChildren("limit")) {
                BsonDocument bsonSortDocument = BsonDocument.parse(request.getFirstChild("sort").strValue());
                prepareBsonQueryData(bsonSortDocument, request.getFirstChild("sort"));
                printlnJson("Query sort", bsonSortDocument);
                int findLimit = request.getFirstChild("limit").intValue();

                iterable = collection.find(new Document()).sort(bsonSortDocument); ///.sort(bsonSortDocument).limit(limitQuery);
            }
            if (request.hasChildren("sort") && !request.hasChildren("limit")) {
                BsonDocument bsonSortDocument = BsonDocument.parse(request.getFirstChild("sort").strValue());
                prepareBsonQueryData(bsonSortDocument, request.getFirstChild("sort"));
                printlnJson("Query sort", bsonSortDocument);
                iterable = collection.find(new Document()).sort(bsonSortDocument);
            }
            if (!request.hasChildren("sort") && request.hasChildren("limit")) {
                int limitQuery = request.getFirstChild("limit").intValue();
                iterable = collection.find(new Document()).limit(limitQuery);
            }
            if (!request.hasChildren("sort") && !request.hasChildren("limit")) {

                iterable = collection.find();
            }

        }
        iterable.forEach(new Block<BsonDocument>() {
            @Override
            public void apply(BsonDocument t) {
                Value queryValue = processQueryRow(t);
                printlnJson("Query document", t);
                v.getChildren("document").add(queryValue);
            }
        });
        showLog();
    } catch (MongoException ex) {
        showLog();

        throw new FaultException("MongoException", ex);

    } catch (JsonParseException ex) {
        showLog();
        throw new FaultException("JsonParseException", ex);
    }

    return v;
}

From source file:joliex.mongodb.MongoDbConnector.java

@RequestResponse
public Value aggregate(Value request) throws FaultException {
    Value v = Value.create();//from  w  ww  .  j ava2 s  .c  o  m
    ArrayList<BsonDocument> bsonAggreagationDocuments = new ArrayList<>();
    String collectionName = request.getFirstChild("collection").strValue();
    MongoCollection<BsonDocument> collection = db.getCollection(collectionName, BsonDocument.class);
    if (request.hasChildren("readConcern")) {
        ReadConcern readConcern = new ReadConcern(
                ReadConcernLevel.fromString(request.getFirstChild("readConcern").strValue()));
        collection.withReadConcern(readConcern);
    }
    for (int counter = 0; counter < request.getChildren("filter").size(); counter++) {
        BsonDocument bsonAggregationDocument = BsonDocument
                .parse(request.getChildren("filter").get(counter).strValue());
        prepareBsonQueryData(bsonAggregationDocument, request.getChildren("filter").get(counter));
        printlnJson("Aggregate filter", bsonAggregationDocument);
        bsonAggreagationDocuments.add(bsonAggregationDocument);
    }
    AggregateIterable<BsonDocument> aggregation = db.getCollection(collectionName)
            .aggregate(bsonAggreagationDocuments, BsonDocument.class).allowDiskUse(Boolean.TRUE);
    aggregation.forEach(new Block<BsonDocument>() {
        @Override
        public void apply(BsonDocument t) {
            printlnJson("Aggregate Document", t);
            Value queryResult = processQueryRow(t);
            v.getChildren("document").add(queryResult);
        }
    });

    return v;
}