Search in sources :

Example 1 with QueryContext

use of org.springframework.data.mongodb.core.QueryOperations.QueryContext in project spring-data-mongodb by spring-projects.

the class ReactiveMongoTemplate method doFind.

/**
 * Map the results of an ad-hoc query on the default MongoDB collection to a List of the specified targetClass while
 * using sourceClass for mapping the query.
 *
 * @since 2.0
 */
<S, T> Flux<T> doFind(String collectionName, Document query, Document fields, Class<S> sourceClass, Class<T> targetClass, FindPublisherPreparer preparer) {
    MongoPersistentEntity<?> entity = mappingContext.getPersistentEntity(sourceClass);
    EntityProjection<T, S> projection = operations.introspectProjection(targetClass, sourceClass);
    QueryContext queryContext = queryOperations.createQueryContext(new BasicQuery(query, fields));
    Document mappedFields = queryContext.getMappedFields(entity, projection);
    Document mappedQuery = queryContext.getMappedQuery(entity);
    if (LOGGER.isDebugEnabled()) {
        LOGGER.debug(String.format("find using query: %s fields: %s for class: %s in collection: %s", serializeToJsonSafely(mappedQuery), mappedFields, sourceClass, collectionName));
    }
    return executeFindMultiInternal(new FindCallback(mappedQuery, mappedFields), preparer, new ProjectingReadCallback<>(mongoConverter, projection, collectionName), collectionName);
}
Also used : BasicQuery(org.springframework.data.mongodb.core.query.BasicQuery) QueryContext(org.springframework.data.mongodb.core.QueryOperations.QueryContext) DistinctQueryContext(org.springframework.data.mongodb.core.QueryOperations.DistinctQueryContext) Document(org.bson.Document) FullDocument(com.mongodb.client.model.changestream.FullDocument)

Example 2 with QueryContext

use of org.springframework.data.mongodb.core.QueryOperations.QueryContext in project spring-data-mongodb by spring-projects.

the class MongoTemplate method exists.

@Override
@SuppressWarnings("ConstantConditions")
public boolean exists(Query query, @Nullable Class<?> entityClass, String collectionName) {
    if (query == null) {
        throw new InvalidDataAccessApiUsageException("Query passed in to exist can't be null");
    }
    Assert.notNull(collectionName, "CollectionName must not be null!");
    QueryContext queryContext = queryOperations.createQueryContext(query);
    Document mappedQuery = queryContext.getMappedQuery(entityClass, this::getPersistentEntity);
    return execute(collectionName, new ExistsCallback(mappedQuery, queryContext.getCollation(entityClass).orElse(null)));
}
Also used : InvalidDataAccessApiUsageException(org.springframework.dao.InvalidDataAccessApiUsageException) QueryContext(org.springframework.data.mongodb.core.QueryOperations.QueryContext) DistinctQueryContext(org.springframework.data.mongodb.core.QueryOperations.DistinctQueryContext) Document(org.bson.Document)

Example 3 with QueryContext

use of org.springframework.data.mongodb.core.QueryOperations.QueryContext in project spring-data-mongodb by spring-projects.

the class MongoTemplate method doFindOne.

/**
 * Map the results of an ad-hoc query on the default MongoDB collection to an object using the template's converter.
 * The query document is specified as a standard {@link Document} and so is the fields specification.
 *
 * @param collectionName name of the collection to retrieve the objects from.
 * @param query the query document that specifies the criteria used to find a record.
 * @param fields the document that specifies the fields to be returned.
 * @param entityClass the parameterized type of the returned list.
 * @param preparer the preparer used to modify the cursor on execution.
 * @return the {@link List} of converted objects.
 * @since 2.2
 */
@SuppressWarnings("ConstantConditions")
protected <T> T doFindOne(String collectionName, Document query, Document fields, CursorPreparer preparer, Class<T> entityClass) {
    MongoPersistentEntity<?> entity = mappingContext.getPersistentEntity(entityClass);
    QueryContext queryContext = queryOperations.createQueryContext(new BasicQuery(query, fields));
    Document mappedFields = queryContext.getMappedFields(entity, EntityProjection.nonProjecting(entityClass));
    Document mappedQuery = queryContext.getMappedQuery(entity);
    if (LOGGER.isDebugEnabled()) {
        LOGGER.debug(String.format("findOne using query: %s fields: %s for class: %s in collection: %s", serializeToJsonSafely(query), mappedFields, entityClass, collectionName));
    }
    return executeFindOneInternal(new FindOneCallback(mappedQuery, mappedFields, preparer), new ReadDocumentCallback<>(this.mongoConverter, entityClass, collectionName), collectionName);
}
Also used : BasicQuery(org.springframework.data.mongodb.core.query.BasicQuery) QueryContext(org.springframework.data.mongodb.core.QueryOperations.QueryContext) DistinctQueryContext(org.springframework.data.mongodb.core.QueryOperations.DistinctQueryContext) Document(org.bson.Document)

Example 4 with QueryContext

use of org.springframework.data.mongodb.core.QueryOperations.QueryContext in project spring-data-mongodb by spring-projects.

the class ReactiveMongoTemplate method doFindOne.

/**
 * Map the results of an ad-hoc query on the default MongoDB collection to an object using the template's converter.
 * The query document is specified as a standard {@link Document} and so is the fields specification.
 *
 * @param collectionName name of the collection to retrieve the objects from.
 * @param query the query document that specifies the criteria used to find a record.
 * @param fields the document that specifies the fields to be returned.
 * @param entityClass the parameterized type of the returned list.
 * @param preparer the preparer modifying collection and publisher to fit the needs.
 * @return the {@link List} of converted objects.
 * @since 2.2
 */
protected <T> Mono<T> doFindOne(String collectionName, Document query, @Nullable Document fields, Class<T> entityClass, FindPublisherPreparer preparer) {
    MongoPersistentEntity<?> entity = mappingContext.getPersistentEntity(entityClass);
    QueryContext queryContext = queryOperations.createQueryContext(new BasicQuery(query, fields != null ? fields : new Document()));
    Document mappedFields = queryContext.getMappedFields(entity, EntityProjection.nonProjecting(entityClass));
    Document mappedQuery = queryContext.getMappedQuery(entity);
    if (LOGGER.isDebugEnabled()) {
        LOGGER.debug(String.format("findOne using query: %s fields: %s for class: %s in collection: %s", serializeToJsonSafely(query), mappedFields, entityClass, collectionName));
    }
    return executeFindOneInternal(new FindOneCallback(mappedQuery, mappedFields, preparer), new ReadDocumentCallback<>(this.mongoConverter, entityClass, collectionName), collectionName);
}
Also used : BasicQuery(org.springframework.data.mongodb.core.query.BasicQuery) QueryContext(org.springframework.data.mongodb.core.QueryOperations.QueryContext) DistinctQueryContext(org.springframework.data.mongodb.core.QueryOperations.DistinctQueryContext) Document(org.bson.Document) FullDocument(com.mongodb.client.model.changestream.FullDocument)

Example 5 with QueryContext

use of org.springframework.data.mongodb.core.QueryOperations.QueryContext in project spring-data-mongodb by spring-projects.

the class ReactiveMongoTemplate method doFind.

protected <S, T> Flux<T> doFind(String collectionName, Document query, Document fields, Class<S> entityClass, @Nullable FindPublisherPreparer preparer, DocumentCallback<T> objectCallback) {
    MongoPersistentEntity<?> entity = mappingContext.getPersistentEntity(entityClass);
    QueryContext queryContext = queryOperations.createQueryContext(new BasicQuery(query, fields));
    Document mappedFields = queryContext.getMappedFields(entity, EntityProjection.nonProjecting(entityClass));
    Document mappedQuery = queryContext.getMappedQuery(entity);
    if (LOGGER.isDebugEnabled()) {
        LOGGER.debug(String.format("find using query: %s fields: %s for class: %s in collection: %s", serializeToJsonSafely(mappedQuery), mappedFields, entityClass, collectionName));
    }
    return executeFindMultiInternal(new FindCallback(mappedQuery, mappedFields), preparer, objectCallback, collectionName);
}
Also used : BasicQuery(org.springframework.data.mongodb.core.query.BasicQuery) QueryContext(org.springframework.data.mongodb.core.QueryOperations.QueryContext) DistinctQueryContext(org.springframework.data.mongodb.core.QueryOperations.DistinctQueryContext) Document(org.bson.Document) FullDocument(com.mongodb.client.model.changestream.FullDocument)

Aggregations

Document (org.bson.Document)9 DistinctQueryContext (org.springframework.data.mongodb.core.QueryOperations.DistinctQueryContext)9 QueryContext (org.springframework.data.mongodb.core.QueryOperations.QueryContext)9 BasicQuery (org.springframework.data.mongodb.core.query.BasicQuery)7 FullDocument (com.mongodb.client.model.changestream.FullDocument)4 ClientSessionOptions (com.mongodb.ClientSessionOptions)1 CursorType (com.mongodb.CursorType)1 MongoException (com.mongodb.MongoException)1 ReadPreference (com.mongodb.ReadPreference)1 WriteConcern (com.mongodb.WriteConcern)1 com.mongodb.client.model (com.mongodb.client.model)1 DeleteResult (com.mongodb.client.result.DeleteResult)1 InsertOneResult (com.mongodb.client.result.InsertOneResult)1 UpdateResult (com.mongodb.client.result.UpdateResult)1 AggregatePublisher (com.mongodb.reactivestreams.client.AggregatePublisher)1 ChangeStreamPublisher (com.mongodb.reactivestreams.client.ChangeStreamPublisher)1 ClientSession (com.mongodb.reactivestreams.client.ClientSession)1 DistinctPublisher (com.mongodb.reactivestreams.client.DistinctPublisher)1 FindPublisher (com.mongodb.reactivestreams.client.FindPublisher)1 MapReducePublisher (com.mongodb.reactivestreams.client.MapReducePublisher)1