org.springframework.data.mongodb.core.ReactiveUpdateOperation Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of spring-data-mongodb Show documentation
Show all versions of spring-data-mongodb Show documentation
MongoDB support for Spring Data
/*
* Copyright 2017-2020 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.data.mongodb.core;
import reactor.core.publisher.Mono;
import org.springframework.data.mongodb.core.aggregation.AggregationUpdate;
import org.springframework.data.mongodb.core.query.CriteriaDefinition;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.data.mongodb.core.query.UpdateDefinition;
import com.mongodb.client.result.UpdateResult;
/**
* {@link ReactiveUpdateOperation} allows creation and execution of reactive MongoDB update / findAndModify /
* findAndReplace operations in a fluent API style.
* The starting {@literal domainType} is used for mapping the {@link Query} provided via {@code matching}, as well as
* the {@link org.springframework.data.mongodb.core.query.Update} via {@code apply} into the MongoDB specific
* representations. The collection to operate on is by default derived from the initial {@literal domainType} and can be
* defined there via {@link org.springframework.data.mongodb.core.mapping.Document}. Using {@code inCollection} allows
* to override the collection name for the execution.
*
*
*
* update(Jedi.class)
* .inCollection("star-wars")
* .matching(query(where("firstname").is("luke")))
* .apply(new Update().set("lastname", "skywalker"))
* .upsert();
*
*
*
* @author Mark Paluch
* @author Christoph Strobl
* @since 2.0
*/
public interface ReactiveUpdateOperation {
/**
* Start creating an update operation for the given {@literal domainType}.
*
* @param domainType must not be {@literal null}.
* @return new instance of {@link ReactiveUpdate}. Never {@literal null}.
* @throws IllegalArgumentException if domainType is {@literal null}.
*/
ReactiveUpdate update(Class domainType);
/**
* Compose findAndModify execution by calling one of the terminating methods.
*/
interface TerminatingFindAndModify {
/**
* Find, modify and return the first matching document.
*
* @return {@link Mono#empty()} if nothing found. Never {@literal null}.
*/
Mono findAndModify();
}
/**
* Compose findAndReplace execution by calling one of the terminating methods.
*
* @author Mark Paluch
* @since 2.1
*/
interface TerminatingFindAndReplace {
/**
* Find, replace and return the first matching document.
*
* @return {@link Mono#empty()} if nothing found. Never {@literal null}.
*/
Mono findAndReplace();
}
/**
* Compose update execution by calling one of the terminating methods.
*/
interface TerminatingUpdate extends TerminatingFindAndModify, FindAndModifyWithOptions {
/**
* Update all matching documents in the collection.
*
* @return never {@literal null}.
*/
Mono all();
/**
* Update the first document in the collection.
*
* @return never {@literal null}.
*/
Mono first();
/**
* Creates a new document if no documents match the filter query or updates the matching ones.
*
* @return never {@literal null}.
*/
Mono upsert();
}
/**
* Declare the {@link org.springframework.data.mongodb.core.query.Update} to apply.
*/
interface UpdateWithUpdate {
/**
* Set the {@link UpdateDefinition} to be applied.
*
* @param update must not be {@literal null}.
* @return new instance of {@link TerminatingUpdate}. Never {@literal null}.
* @throws IllegalArgumentException if update is {@literal null}.
* @since 3.0
* @see Update
* @see AggregationUpdate
*/
TerminatingUpdate apply(UpdateDefinition update);
/**
* Specify {@code replacement} object.
*
* @param replacement must not be {@literal null}.
* @return new instance of {@link FindAndReplaceOptions}.
* @throws IllegalArgumentException if options is {@literal null}.
* @since 2.1
*/
FindAndReplaceWithProjection replaceWith(T replacement);
}
/**
* Explicitly define the name of the collection to perform operation in (optional).
*/
interface UpdateWithCollection {
/**
* Explicitly set the name of the collection to perform the query on.
* Skip this step to use the default collection derived from the domain type.
*
* @param collection must not be {@literal null} nor {@literal empty}.
* @return new instance of {@link UpdateWithCollection}. Never {@literal null}.
* @throws IllegalArgumentException if collection is {@literal null} or empty.
*/
UpdateWithQuery inCollection(String collection);
}
/**
* Define a filter query for the {@link org.springframework.data.mongodb.core.query.Update} (optional).
*/
interface UpdateWithQuery extends UpdateWithUpdate {
/**
* Filter documents by given {@literal query}.
*
* @param query must not be {@literal null}.
* @return new instance of {@link UpdateWithQuery}. Never {@literal null}.
* @throws IllegalArgumentException if query is {@literal null}.
*/
UpdateWithUpdate matching(Query query);
/**
* Set the filter {@link CriteriaDefinition criteria} to be used.
*
* @param criteria must not be {@literal null}.
* @return new instance of {@link UpdateWithUpdate}.
* @throws IllegalArgumentException if query is {@literal null}.
* @since 3.0
*/
default UpdateWithUpdate matching(CriteriaDefinition criteria) {
return matching(Query.query(criteria));
}
}
/**
* Define {@link FindAndModifyOptions} (optional).
*/
interface FindAndModifyWithOptions {
/**
* Explicitly define {@link FindAndModifyOptions} for the
* {@link org.springframework.data.mongodb.core.query.Update}.
*
* @param options must not be {@literal null}.
* @return new instance of {@link TerminatingFindAndModify}. Never {@literal null}.
* @throws IllegalArgumentException if options is {@literal null}.
*/
TerminatingFindAndModify withOptions(FindAndModifyOptions options);
}
/**
* Define {@link FindAndReplaceOptions}.
*
* @author Mark Paluch
* @author Christoph Strobl
* @since 2.1
*/
interface FindAndReplaceWithOptions extends TerminatingFindAndReplace {
/**
* Explicitly define {@link FindAndReplaceOptions} for the {@link Update}.
*
* @param options must not be {@literal null}.
* @return new instance of {@link FindAndReplaceOptions}.
* @throws IllegalArgumentException if options is {@literal null}.
*/
FindAndReplaceWithProjection withOptions(FindAndReplaceOptions options);
}
/**
* Result type override (Optional).
*
* @author Christoph Strobl
* @since 2.1
*/
interface FindAndReplaceWithProjection extends FindAndReplaceWithOptions {
/**
* Define the target type fields should be mapped to.
* Skip this step if you are anyway only interested in the original domain type.
*
* @param resultType must not be {@literal null}.
* @param result type.
* @return new instance of {@link FindAndReplaceWithProjection}.
* @throws IllegalArgumentException if resultType is {@literal null}.
*/
FindAndReplaceWithOptions as(Class resultType);
}
interface ReactiveUpdate extends UpdateWithCollection, UpdateWithQuery, UpdateWithUpdate {}
}