Search in sources :

Example 1 with MongoDBResult

use of org.apache.gora.mongodb.query.MongoDBResult in project gora by apache.

the class MongoStore method execute.

/**
 * Execute the query and return the result.
 */
@Override
public Result<K, T> execute(final Query<K, T> query) throws GoraException {
    try {
        String[] fields = getFieldsToQuery(query.getFields());
        // Build the actual MongoDB query
        Bson q = MongoDBQuery.toDBQuery(query);
        Bson p = MongoDBQuery.toProjection(fields, mapping);
        if (query.getFilter() != null) {
            Optional<Bson> filter = filterUtil.setFilter(query.getFilter(), this);
            if (!filter.isPresent()) {
                // don't need local filter
                query.setLocalFilterEnabled(false);
            } else {
                q = and(q, filter.get());
            }
        }
        // Execute the query on the collection
        FindIterable<Document> iterable = mongoClientColl.find(q).projection(p);
        CountOptions countOptions = new CountOptions();
        if (query.getLimit() > 0) {
            iterable.limit((int) query.getLimit());
            countOptions.limit((int) query.getLimit());
        }
        iterable.batchSize(100);
        iterable.noCursorTimeout(true);
        // Build the result
        long size = mongoClientColl.countDocuments(q, countOptions);
        return new MongoDBResult<>(this, query, iterable.cursor(), size);
    } catch (Exception e) {
        throw new GoraException(e);
    }
}
Also used : GoraException(org.apache.gora.util.GoraException) MongoDBResult(org.apache.gora.mongodb.query.MongoDBResult) CountOptions(com.mongodb.client.model.CountOptions) Document(org.bson.Document) GoraException(org.apache.gora.util.GoraException) IOException(java.io.IOException) UnknownHostException(java.net.UnknownHostException) Bson(org.bson.conversions.Bson)

Aggregations

CountOptions (com.mongodb.client.model.CountOptions)1 IOException (java.io.IOException)1 UnknownHostException (java.net.UnknownHostException)1 MongoDBResult (org.apache.gora.mongodb.query.MongoDBResult)1 GoraException (org.apache.gora.util.GoraException)1 Document (org.bson.Document)1 Bson (org.bson.conversions.Bson)1