Search in sources :

Example 6 with UpdateContext

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

the class MongoTemplate method doFindAndModify.

@SuppressWarnings("ConstantConditions")
protected <T> T doFindAndModify(String collectionName, Document query, Document fields, Document sort, Class<T> entityClass, UpdateDefinition update, @Nullable FindAndModifyOptions options) {
    EntityReader<? super T, Bson> readerToUse = this.mongoConverter;
    if (options == null) {
        options = new FindAndModifyOptions();
    }
    MongoPersistentEntity<?> entity = mappingContext.getPersistentEntity(entityClass);
    UpdateContext updateContext = queryOperations.updateSingleContext(update, query, false);
    updateContext.increaseVersionForUpdateIfNecessary(entity);
    Document mappedQuery = updateContext.getMappedQuery(entity);
    Object mappedUpdate = updateContext.isAggregationUpdate() ? updateContext.getUpdatePipeline(entityClass) : updateContext.getMappedUpdate(entity);
    if (LOGGER.isDebugEnabled()) {
        LOGGER.debug(String.format("findAndModify using query: %s fields: %s sort: %s for class: %s and update: %s in collection: %s", serializeToJsonSafely(mappedQuery), fields, sort, entityClass, serializeToJsonSafely(mappedUpdate), collectionName));
    }
    return executeFindOneInternal(new FindAndModifyCallback(mappedQuery, fields, sort, mappedUpdate, update.getArrayFilters().stream().map(ArrayFilter::asDocument).collect(Collectors.toList()), options), new ReadDocumentCallback<>(readerToUse, entityClass, collectionName), collectionName);
}
Also used : UpdateContext(org.springframework.data.mongodb.core.QueryOperations.UpdateContext) Document(org.bson.Document) Bson(org.bson.conversions.Bson)

Aggregations

Document (org.bson.Document)6 UpdateContext (org.springframework.data.mongodb.core.QueryOperations.UpdateContext)6 WriteConcern (com.mongodb.WriteConcern)4 FullDocument (com.mongodb.client.model.changestream.FullDocument)3 Bson (org.bson.conversions.Bson)3 ClientSessionOptions (com.mongodb.ClientSessionOptions)2 CursorType (com.mongodb.CursorType)2 MongoException (com.mongodb.MongoException)2 ReadPreference (com.mongodb.ReadPreference)2 com.mongodb.client.model (com.mongodb.client.model)2 DeleteResult (com.mongodb.client.result.DeleteResult)2 InsertOneResult (com.mongodb.client.result.InsertOneResult)2 UpdateResult (com.mongodb.client.result.UpdateResult)2 AggregatePublisher (com.mongodb.reactivestreams.client.AggregatePublisher)2 ChangeStreamPublisher (com.mongodb.reactivestreams.client.ChangeStreamPublisher)2 ClientSession (com.mongodb.reactivestreams.client.ClientSession)2 DistinctPublisher (com.mongodb.reactivestreams.client.DistinctPublisher)2 FindPublisher (com.mongodb.reactivestreams.client.FindPublisher)2 MapReducePublisher (com.mongodb.reactivestreams.client.MapReducePublisher)2 MongoClient (com.mongodb.reactivestreams.client.MongoClient)2