Search in sources :

Example 1 with MongoFindOptions

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;
}
Also used : DelegateStoredQuery(io.micronaut.data.runtime.query.internal.DelegateStoredQuery) Sort(io.micronaut.data.model.Sort) MongoFindOptions(io.micronaut.data.mongodb.operations.options.MongoFindOptions) DefaultPreparedQuery(io.micronaut.data.runtime.query.internal.DefaultPreparedQuery) Internal(io.micronaut.core.annotation.Internal) Collectors(java.util.stream.Collectors) PreparedQuery(io.micronaut.data.model.runtime.PreparedQuery) BsonDocument(org.bson.BsonDocument) ArrayList(java.util.ArrayList) Bson(org.bson.conversions.Bson) List(java.util.List) Sorts(com.mongodb.client.model.Sorts) Pageable(io.micronaut.data.model.Pageable) RuntimePersistentEntity(io.micronaut.data.model.runtime.RuntimePersistentEntity) BsonInt32(org.bson.BsonInt32) Pageable(io.micronaut.data.model.Pageable) MongoFindOptions(io.micronaut.data.mongodb.operations.options.MongoFindOptions) Sort(io.micronaut.data.model.Sort) Bson(org.bson.conversions.Bson)

Example 2 with MongoFindOptions

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));
    }
}
Also used : DataType(io.micronaut.data.model.DataType) BeanContext(io.micronaut.context.BeanContext) LoggerFactory(org.slf4j.LoggerFactory) Tuples(reactor.util.function.Tuples) MongoCollection(com.mongodb.reactivestreams.client.MongoCollection) Internal(io.micronaut.core.annotation.Internal) ReactiveCascadeOperations(io.micronaut.data.runtime.operations.internal.ReactiveCascadeOperations) NoTransactionException(io.micronaut.transaction.exceptions.NoTransactionException) BsonValue(org.bson.BsonValue) EachBean(io.micronaut.context.annotation.EachBean) StoredQuery(io.micronaut.data.model.runtime.StoredQuery) Map(java.util.Map) DataAccessException(io.micronaut.data.exceptions.DataAccessException) DeleteOperation(io.micronaut.data.model.runtime.DeleteOperation) RuntimeAssociation(io.micronaut.data.model.runtime.RuntimeAssociation) CursorType(com.mongodb.CursorType) DeleteOneModel(com.mongodb.client.model.DeleteOneModel) PersistentProperty(io.micronaut.data.model.PersistentProperty) TransactionDefinition(io.micronaut.transaction.TransactionDefinition) RuntimePersistentProperty(io.micronaut.data.model.runtime.RuntimePersistentProperty) Predicate(java.util.function.Predicate) Qualifiers(io.micronaut.inject.qualifiers.Qualifiers) BsonDocumentWrapper(org.bson.BsonDocumentWrapper) Collectors(java.util.stream.Collectors) ReactiveMongoDatabaseFactory(io.micronaut.data.mongodb.database.ReactiveMongoDatabaseFactory) PreparedQuery(io.micronaut.data.model.runtime.PreparedQuery) Serializable(java.io.Serializable) Objects(java.util.Objects) List(java.util.List) ReactiveTransactionStatus(io.micronaut.transaction.reactive.ReactiveTransactionStatus) FindPublisher(com.mongodb.reactivestreams.client.FindPublisher) Dialect(io.micronaut.data.model.query.builder.sql.Dialect) TransactionUsageException(io.micronaut.transaction.exceptions.TransactionUsageException) MongoAggregationOptions(io.micronaut.data.mongodb.operations.options.MongoAggregationOptions) ReplaceOneModel(com.mongodb.client.model.ReplaceOneModel) Parameter(io.micronaut.context.annotation.Parameter) BeanProperty(io.micronaut.core.beans.BeanProperty) AbstractReactiveEntitiesOperations(io.micronaut.data.runtime.operations.internal.AbstractReactiveEntitiesOperations) Tuple2(reactor.util.function.Tuple2) RuntimeEntityRegistry(io.micronaut.data.model.runtime.RuntimeEntityRegistry) CodecRegistry(org.bson.codecs.configuration.CodecRegistry) Function(java.util.function.Function) MongoClient(com.mongodb.reactivestreams.client.MongoClient) ReactorReactiveRepositoryOperations(io.micronaut.data.operations.reactive.ReactorReactiveRepositoryOperations) BsonDocument(org.bson.BsonDocument) ArrayList(java.util.ArrayList) Filters(com.mongodb.client.model.Filters) Bson(org.bson.conversions.Bson) MongoDatabase(com.mongodb.reactivestreams.client.MongoDatabase) OperationContext(io.micronaut.data.runtime.operations.internal.OperationContext) Nullable(io.micronaut.core.annotation.Nullable) Page(io.micronaut.data.model.Page) Argument(io.micronaut.core.type.Argument) RequiresReactiveMongo(io.micronaut.data.mongodb.conf.RequiresReactiveMongo) AttributeConverterRegistry(io.micronaut.data.model.runtime.AttributeConverterRegistry) DateTimeProvider(io.micronaut.data.runtime.date.DateTimeProvider) Collation(com.mongodb.client.model.Collation) MediaTypeCodec(io.micronaut.http.codec.MediaTypeCodec) PagedQuery(io.micronaut.data.model.runtime.PagedQuery) Logger(org.slf4j.Logger) UpdateOneModel(com.mongodb.client.model.UpdateOneModel) MongoFindOptions(io.micronaut.data.mongodb.operations.options.MongoFindOptions) Publisher(org.reactivestreams.Publisher) DataConversionService(io.micronaut.data.runtime.convert.DataConversionService) Mono(reactor.core.publisher.Mono) ReactiveTransactionOperations(io.micronaut.transaction.reactive.ReactiveTransactionOperations) ClientSession(com.mongodb.reactivestreams.client.ClientSession) InsertBatchOperation(io.micronaut.data.model.runtime.InsertBatchOperation) TimeUnit(java.util.concurrent.TimeUnit) NonNull(io.micronaut.core.annotation.NonNull) Flux(reactor.core.publisher.Flux) AggregatePublisher(com.mongodb.reactivestreams.client.AggregatePublisher) ConversionContext(io.micronaut.core.convert.ConversionContext) DeleteResult(com.mongodb.client.result.DeleteResult) DeleteBatchOperation(io.micronaut.data.model.runtime.DeleteBatchOperation) InsertOperation(io.micronaut.data.model.runtime.InsertOperation) AnnotationMetadata(io.micronaut.core.annotation.AnnotationMetadata) TransactionSystemException(io.micronaut.transaction.exceptions.TransactionSystemException) RuntimePersistentEntity(io.micronaut.data.model.runtime.RuntimePersistentEntity) AbstractReactiveEntityOperations(io.micronaut.data.runtime.operations.internal.AbstractReactiveEntityOperations) UpdateBatchOperation(io.micronaut.data.model.runtime.UpdateBatchOperation) UpdateOperation(io.micronaut.data.model.runtime.UpdateOperation) DataSettings(io.micronaut.data.runtime.config.DataSettings) Collections(java.util.Collections) MongoFindOptions(io.micronaut.data.mongodb.operations.options.MongoFindOptions) BsonDocument(org.bson.BsonDocument) MongoDatabase(com.mongodb.reactivestreams.client.MongoDatabase) Bson(org.bson.conversions.Bson)

Example 3 with MongoFindOptions

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);
    }
}
Also used : InsertManyResult(com.mongodb.client.result.InsertManyResult) DataType(io.micronaut.data.model.DataType) BeanContext(io.micronaut.context.BeanContext) Spliterators(java.util.Spliterators) MongoDatabase(com.mongodb.client.MongoDatabase) ExecutorReactiveOperations(io.micronaut.data.runtime.operations.ExecutorReactiveOperations) Internal(io.micronaut.core.annotation.Internal) BsonValue(org.bson.BsonValue) EachBean(io.micronaut.context.annotation.EachBean) StoredQuery(io.micronaut.data.model.runtime.StoredQuery) UpdateResult(com.mongodb.client.result.UpdateResult) Map(java.util.Map) DataAccessException(io.micronaut.data.exceptions.DataAccessException) DeleteOperation(io.micronaut.data.model.runtime.DeleteOperation) RuntimeAssociation(io.micronaut.data.model.runtime.RuntimeAssociation) CursorType(com.mongodb.CursorType) AsyncCapableRepository(io.micronaut.data.operations.async.AsyncCapableRepository) DeleteOneModel(com.mongodb.client.model.DeleteOneModel) PersistentProperty(io.micronaut.data.model.PersistentProperty) InsertOneResult(com.mongodb.client.result.InsertOneResult) RuntimePersistentProperty(io.micronaut.data.model.runtime.RuntimePersistentProperty) Predicate(java.util.function.Predicate) AggregateIterable(com.mongodb.client.AggregateIterable) Qualifiers(io.micronaut.inject.qualifiers.Qualifiers) BsonDocumentWrapper(org.bson.BsonDocumentWrapper) Collectors(java.util.stream.Collectors) Executors(java.util.concurrent.Executors) PreparedQuery(io.micronaut.data.model.runtime.PreparedQuery) Serializable(java.io.Serializable) List(java.util.List) Stream(java.util.stream.Stream) FindIterable(com.mongodb.client.FindIterable) Dialect(io.micronaut.data.model.query.builder.sql.Dialect) Optional(java.util.Optional) MongoSynchronousTransactionManager(io.micronaut.data.mongodb.transaction.MongoSynchronousTransactionManager) Spliterator(java.util.Spliterator) MongoAggregationOptions(io.micronaut.data.mongodb.operations.options.MongoAggregationOptions) Named(jakarta.inject.Named) MongoDatabaseFactory(io.micronaut.data.mongodb.database.MongoDatabaseFactory) ReplaceOneModel(com.mongodb.client.model.ReplaceOneModel) Parameter(io.micronaut.context.annotation.Parameter) MongoClient(com.mongodb.client.MongoClient) MongoCollection(com.mongodb.client.MongoCollection) BeanProperty(io.micronaut.core.beans.BeanProperty) RuntimeEntityRegistry(io.micronaut.data.model.runtime.RuntimeEntityRegistry) CodecRegistry(org.bson.codecs.configuration.CodecRegistry) RequiresSyncMongo(io.micronaut.data.mongodb.conf.RequiresSyncMongo) Function(java.util.function.Function) BsonDocument(org.bson.BsonDocument) ArrayList(java.util.ArrayList) Filters(com.mongodb.client.model.Filters) Bson(org.bson.conversions.Bson) MongoCursor(com.mongodb.client.MongoCursor) OperationContext(io.micronaut.data.runtime.operations.internal.OperationContext) Nullable(io.micronaut.core.annotation.Nullable) Page(io.micronaut.data.model.Page) Pageable(io.micronaut.data.model.Pageable) ReactiveCapableRepository(io.micronaut.data.operations.reactive.ReactiveCapableRepository) ExecutorAsyncOperations(io.micronaut.data.runtime.operations.ExecutorAsyncOperations) StreamSupport(java.util.stream.StreamSupport) AttributeConverterRegistry(io.micronaut.data.model.runtime.AttributeConverterRegistry) ReactiveRepositoryOperations(io.micronaut.data.operations.reactive.ReactiveRepositoryOperations) DateTimeProvider(io.micronaut.data.runtime.date.DateTimeProvider) ExecutorService(java.util.concurrent.ExecutorService) Collation(com.mongodb.client.model.Collation) MediaTypeCodec(io.micronaut.http.codec.MediaTypeCodec) PagedQuery(io.micronaut.data.model.runtime.PagedQuery) UpdateOneModel(com.mongodb.client.model.UpdateOneModel) MongoFindOptions(io.micronaut.data.mongodb.operations.options.MongoFindOptions) AbstractSyncEntityOperations(io.micronaut.data.runtime.operations.internal.AbstractSyncEntityOperations) DataConversionService(io.micronaut.data.runtime.convert.DataConversionService) InsertBatchOperation(io.micronaut.data.model.runtime.InsertBatchOperation) SyncCascadeOperations(io.micronaut.data.runtime.operations.internal.SyncCascadeOperations) TimeUnit(java.util.concurrent.TimeUnit) Consumer(java.util.function.Consumer) NonNull(io.micronaut.core.annotation.NonNull) BulkWriteResult(com.mongodb.bulk.BulkWriteResult) DeleteResult(com.mongodb.client.result.DeleteResult) DeleteBatchOperation(io.micronaut.data.model.runtime.DeleteBatchOperation) InsertOperation(io.micronaut.data.model.runtime.InsertOperation) AbstractSyncEntitiesOperations(io.micronaut.data.runtime.operations.internal.AbstractSyncEntitiesOperations) AnnotationMetadata(io.micronaut.core.annotation.AnnotationMetadata) ClientSession(com.mongodb.client.ClientSession) RuntimePersistentEntity(io.micronaut.data.model.runtime.RuntimePersistentEntity) MongoIterable(com.mongodb.client.MongoIterable) UpdateBatchOperation(io.micronaut.data.model.runtime.UpdateBatchOperation) UpdateOperation(io.micronaut.data.model.runtime.UpdateOperation) Collections(java.util.Collections) MongoFindOptions(io.micronaut.data.mongodb.operations.options.MongoFindOptions) BsonDocument(org.bson.BsonDocument) MongoDatabase(com.mongodb.client.MongoDatabase) Bson(org.bson.conversions.Bson)

Example 4 with MongoFindOptions

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);
    }
}
Also used : MongoFindOptions(io.micronaut.data.mongodb.operations.options.MongoFindOptions) Collation(com.mongodb.client.model.Collation) Bson(org.bson.conversions.Bson)

Aggregations

MongoFindOptions (io.micronaut.data.mongodb.operations.options.MongoFindOptions)4 Bson (org.bson.conversions.Bson)4 Collation (com.mongodb.client.model.Collation)3 Internal (io.micronaut.core.annotation.Internal)3 CursorType (com.mongodb.CursorType)2 DeleteOneModel (com.mongodb.client.model.DeleteOneModel)2 Filters (com.mongodb.client.model.Filters)2 ReplaceOneModel (com.mongodb.client.model.ReplaceOneModel)2 UpdateOneModel (com.mongodb.client.model.UpdateOneModel)2 DeleteResult (com.mongodb.client.result.DeleteResult)2 BeanContext (io.micronaut.context.BeanContext)2 EachBean (io.micronaut.context.annotation.EachBean)2 Parameter (io.micronaut.context.annotation.Parameter)2 AnnotationMetadata (io.micronaut.core.annotation.AnnotationMetadata)2 NonNull (io.micronaut.core.annotation.NonNull)2 Nullable (io.micronaut.core.annotation.Nullable)2 BeanProperty (io.micronaut.core.beans.BeanProperty)2 DataAccessException (io.micronaut.data.exceptions.DataAccessException)2 DataType (io.micronaut.data.model.DataType)2 Page (io.micronaut.data.model.Page)2