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);
}
}
Aggregations