use of io.micronaut.data.mongodb.operations.options.MongoFindOptions in project micronaut-data by micronaut-projects.
the class DefaultMongoPreparedQuery method getFind.
@Override
public MongoFind getFind() {
MongoFind find = mongoStoredQuery.getFind(preparedQuery.getContext());
Pageable pageable = preparedQuery.getPageable();
if (pageable != Pageable.UNPAGED) {
MongoFindOptions findOptions = find.getOptions();
MongoFindOptions options = findOptions == null ? new MongoFindOptions() : new MongoFindOptions(findOptions);
options.limit(pageable.getSize()).skip((int) pageable.getOffset());
Sort pageableSort = pageable.getSort();
if (pageableSort.isSorted()) {
Bson sort = pageableSort.getOrderBy().stream().map(order -> order.isAscending() ? Sorts.ascending(order.getProperty()) : Sorts.descending(order.getProperty())).collect(Collectors.collectingAndThen(Collectors.toList(), Sorts::orderBy));
options.sort(sort);
}
return new MongoFind(options);
}
return find;
}
use of io.micronaut.data.mongodb.operations.options.MongoFindOptions in project micronaut-data by micronaut-projects.
the class DefaultReactiveMongoRepositoryOperations method getCount.
private <T, R> Mono<R> getCount(ClientSession clientSession, MongoPreparedQuery<T, R, MongoDatabase> preparedQuery) {
Class<R> resultType = preparedQuery.getResultType();
MongoDatabase database = preparedQuery.getDatabase();
RuntimePersistentEntity<T> persistentEntity = preparedQuery.getRuntimePersistentEntity();
if (preparedQuery.isAggregate()) {
MongoAggregation aggregation = preparedQuery.getAggregation();
if (QUERY_LOG.isDebugEnabled()) {
QUERY_LOG.debug("Executing Mongo 'aggregate' with pipeline: {}", aggregation.getPipeline().stream().map(e -> e.toBsonDocument().toJson()).collect(Collectors.toList()));
}
return Mono.from(aggregate(clientSession, preparedQuery, BsonDocument.class).first()).map(bsonDocument -> convertResult(database.getCodecRegistry(), resultType, bsonDocument, false)).switchIfEmpty(Mono.defer(() -> Mono.just(conversionService.convertRequired(0, resultType))));
} else {
MongoFind find = preparedQuery.getFind();
MongoFindOptions options = find.getOptions();
Bson filter = options == null ? null : options.getFilter();
filter = filter == null ? new BsonDocument() : filter;
if (QUERY_LOG.isDebugEnabled()) {
QUERY_LOG.debug("Executing Mongo 'countDocuments' with filter: {}", filter.toBsonDocument().toJson());
}
return Mono.from(getCollection(database, persistentEntity, BsonDocument.class).countDocuments(clientSession, filter)).map(count -> conversionService.convertRequired(count, resultType));
}
}
use of io.micronaut.data.mongodb.operations.options.MongoFindOptions in project micronaut-data by micronaut-projects.
the class DefaultMongoRepositoryOperations method getCount.
private <T, R> R getCount(ClientSession clientSession, MongoPreparedQuery<T, R, MongoDatabase> preparedQuery) {
Class<R> resultType = preparedQuery.getResultType();
RuntimePersistentEntity<T> persistentEntity = preparedQuery.getRuntimePersistentEntity();
MongoDatabase database = preparedQuery.getDatabase();
if (preparedQuery.isAggregate()) {
MongoAggregation aggregation = preparedQuery.getAggregation();
if (QUERY_LOG.isDebugEnabled()) {
QUERY_LOG.debug("Executing Mongo 'aggregate' with pipeline: {}", aggregation.getPipeline().stream().map(e -> e.toBsonDocument().toJson()).collect(Collectors.toList()));
}
R result = aggregate(clientSession, preparedQuery, BsonDocument.class).map(bsonDocument -> convertResult(database.getCodecRegistry(), resultType, bsonDocument, false)).first();
if (result == null) {
result = conversionService.convertRequired(0, resultType);
}
return result;
} else {
MongoFind find = preparedQuery.getFind();
MongoFindOptions options = find.getOptions();
Bson filter = options == null ? null : options.getFilter();
filter = filter == null ? new BsonDocument() : filter;
if (QUERY_LOG.isDebugEnabled()) {
QUERY_LOG.debug("Executing Mongo 'countDocuments' with filter: {}", filter.toBsonDocument().toJson());
}
long count = getCollection(database, persistentEntity, BsonDocument.class).countDocuments(clientSession, filter);
return conversionService.convertRequired(count, resultType);
}
}
use of io.micronaut.data.mongodb.operations.options.MongoFindOptions in project micronaut-data by micronaut-projects.
the class AbstractMongoRepositoryOperations method logFind.
protected void logFind(MongoFind find) {
StringBuilder sb = new StringBuilder();
MongoFindOptions options = find.getOptions();
if (options != null) {
Bson filter = options.getFilter();
if (filter != null) {
sb.append(" filter: ").append(filter.toBsonDocument().toJson());
}
Bson sort = options.getSort();
if (sort != null) {
sb.append(" sort: ").append(sort.toBsonDocument().toJson());
}
Bson projection = options.getProjection();
if (projection != null) {
sb.append(" projection: ").append(projection.toBsonDocument().toJson());
}
Collation collation = options.getCollation();
if (collation != null) {
sb.append(" collation: ").append(collation);
}
}
if (sb.length() == 0) {
QUERY_LOG.debug("Executing exists Mongo 'find'");
} else {
QUERY_LOG.debug("Executing exists Mongo 'find' with" + sb);
}
}
Aggregations