下面列出了怎么用com.mongodb.client.model.FindOneAndReplaceOptions的API类实例代码及写法,或者点击链接到github查看源代码。
/**
* Updates the document if the document's ETAG is matching the given etag (conditional put).
* <p>
* Using this method requires that the document contains an "etag" field that is updated if
* the document is changed.
* </p>
*
* @param value the new value
* @param eTag the etag used for conditional update
* @param maxTime max time for the update
* @param timeUnit the time unit for the maxTime value
* @return {@link UpdateIfMatchResult}
*/
public UpdateIfMatchResult updateIfMatch(final V value,
final String eTag,
final long maxTime,
final TimeUnit timeUnit) {
final K key = keyOf(value);
if (key != null) {
final Bson query = and(eq(AbstractMongoRepository.ID, key), eq(ETAG, eTag));
final Document updatedETaggable = collectionWithWriteTimeout(maxTime, timeUnit).findOneAndReplace(query, encode(value), new FindOneAndReplaceOptions().returnDocument(AFTER));
if (isNull(updatedETaggable)) {
final boolean documentExists = collection()
.countDocuments(eq(AbstractMongoRepository.ID, key), new CountOptions().maxTime(maxTime, timeUnit)) != 0;
if (documentExists) {
return CONCURRENTLY_MODIFIED;
}
return NOT_FOUND;
}
return OK;
} else {
throw new IllegalArgumentException("Key must not be null");
}
}
protected final FluentFuture<Optional<T>> doReplace(
final Constraints.ConstraintHost criteria,
final T document,
final FindOneAndReplaceOptions options) {
checkNotNull(criteria, "criteria");
checkNotNull(document, "document");
checkNotNull(options, "options");
return submit(new Callable<Optional<T>>() {
@Override
public Optional<T> call() throws Exception {
@Nullable T result = collection().findOneAndReplace(
convertToBson(criteria), // query
document,
options);
return Optional.fromNullable(result);
}
});
}
@Test
void findAndReplace() {
ReactiveMongoDatabase database = client.getDatabase(DATABASE);
ReactiveMongoCollection<Document> collection = database.getCollection("test");
CompletableFuture.allOf(
collection
.insertOne(new Document("id", 1).append("name", "superman").append("type", "heroes")
.append("stars", 5))
.subscribeAsCompletionStage(),
collection.insertOne(
new Document("id", 2).append("name", "batman").append("type", "heroes").append("stars", 4))
.subscribeAsCompletionStage(),
collection
.insertOne(new Document("id", 3).append("name", "frogman").append("type", "villain")
.append("stars", 1))
.subscribeAsCompletionStage(),
collection.insertOne(
new Document("id", 4).append("name", "joker").append("type", "villain").append("stars", 5))
.subscribeAsCompletionStage())
.join();
Document newVillain = new Document("id", 5).append("name", "lex lutor").append("type", "villain")
.append("stars", 3);
Document newHeroes = new Document("id", 6).append("name", "supergirl").append("type", "heroes")
.append("stars", 2);
Document frogman = collection.findOneAndReplace(new Document("id", 3), newVillain).await().indefinitely();
Document supergirl = collection.findOneAndReplace(new Document("id", 2), newHeroes,
new FindOneAndReplaceOptions().returnDocument(ReturnDocument.AFTER)).await().indefinitely();
assertThat(frogman).contains(entry("stars", 1), entry("name", "frogman"));
assertThat(supergirl).contains(entry("stars", 2), entry("name", "supergirl"));
}
@Override
public Observable<TDocument> findOneAndReplace(final Bson filter, final TDocument replacement, final FindOneAndReplaceOptions options) {
return RxObservables.create(Observables.observe(new Block<SingleResultCallback<TDocument>>() {
@Override
public void apply(final SingleResultCallback<TDocument> callback) {
wrapped.findOneAndReplace(filter, replacement, options, callback);
}
}), observableAdapter);
}
@Override
public Publisher<TDocument> findOneAndReplace(final Bson filter, final TDocument replacement, final FindOneAndReplaceOptions options) {
return new ObservableToPublisher<TDocument>(com.mongodb.async.client.Observables.observe(
new Block<com.mongodb.async.SingleResultCallback<TDocument>>() {
@Override
public void apply(final com.mongodb.async.SingleResultCallback<TDocument> callback) {
wrapped.findOneAndReplace(filter, replacement, options, callback);
}
}));
}
@Override
public Publisher<TDocument> findOneAndReplace(final ClientSession clientSession, final Bson filter, final TDocument replacement,
final FindOneAndReplaceOptions options) {
return new ObservableToPublisher<TDocument>(com.mongodb.async.client.Observables.observe(
new Block<com.mongodb.async.SingleResultCallback<TDocument>>() {
@Override
public void apply(final com.mongodb.async.SingleResultCallback<TDocument> callback) {
wrapped.findOneAndReplace(clientSession.getWrapped(), filter, replacement, options, callback);
}
}));
}
protected Replacer(
Repository<T> repository,
T document,
Constraints.ConstraintHost criteria,
Constraints.Constraint ordering) {
super(repository);
this.document = checkNotNull(document, "document");
this.criteria = checkNotNull(criteria, "criteria");
this.ordering = checkNotNull(ordering, "ordering");
this.options = new FindOneAndReplaceOptions();
}
@Override
public Uni<T> findOneAndReplace(Bson filter, T replacement, FindOneAndReplaceOptions options) {
return Wrappers.toUni(collection.findOneAndReplace(filter, replacement, options));
}
@Override
public Uni<T> findOneAndReplace(ClientSession clientSession, Bson filter, T replacement,
FindOneAndReplaceOptions options) {
return Wrappers.toUni(collection.findOneAndReplace(clientSession, filter, replacement, options));
}
@Override
public Observable<TDocument> findOneAndReplace(final Bson filter, final TDocument replacement) {
return findOneAndReplace(filter, replacement, new FindOneAndReplaceOptions());
}
@Override
public Publisher<TDocument> findOneAndReplace(final Bson filter, final TDocument replacement) {
return findOneAndReplace(filter, replacement, new FindOneAndReplaceOptions());
}
@Override
public Publisher<TDocument> findOneAndReplace(final ClientSession clientSession, final Bson filter, final TDocument replacement) {
return findOneAndReplace(clientSession, filter, replacement, new FindOneAndReplaceOptions());
}
/**
* Atomically find a document and replace it.
*
* @param filter the query filter to apply the the replace operation
* @param replacement the replacement document
* @param options the options to apply to the operation
* @return a {@link Uni} completed with the document that was replaced. Depending on the value of the
* {@code returnOriginal}
* property, this will either be the document as it was before the update or as it is after the update. If no
* documents matched the
* query filter, then the uni is completed with {@code null}.
*/
Uni<T> findOneAndReplace(Bson filter, T replacement, FindOneAndReplaceOptions options);
/**
* Atomically find a document and replace it.
*
* @param clientSession the client session with which to associate this operation
* @param filter the query filter to apply the the replace operation
* @param replacement the replacement document
* @param options the options to apply to the operation
* @return a {@link Uni} completed with the document that was replaced. Depending on the value of the
* {@code returnOriginal}
* property, this will either be the document as it was before the update or as it is after the update. If no
* documents matched the
* query filter, then the uni is completed with {@code null}.
*/
Uni<T> findOneAndReplace(ClientSession clientSession, Bson filter, T replacement,
FindOneAndReplaceOptions options);
/**
* Atomically find a document and replace it.
*
* @param filter the query filter to apply the the replace operation
* @param replacement the replacement document
* @param options the options to apply to the operation
* @return an Observable with a single element the document that was replaced. Depending on the value of the {@code returnOriginal}
* property, this will either be the document as it was before the update or as it is after the update.
* If no documents matched the query filter, then the observer will complete without emitting any items
*/
Observable<TDocument> findOneAndReplace(Bson filter, TDocument replacement, FindOneAndReplaceOptions options);
/**
* Atomically find a document and replace it.
*
* @param filter the query filter to apply the the replace operation
* @param replacement the replacement document
* @param options the options to apply to the operation
* @return a publisher with a single element the document that was replaced. Depending on the value of the {@code returnOriginal}
* property, this will either be the document as it was before the update or as it is after the update. If no documents matched the
* query filter, then null will be returned
*/
Publisher<TDocument> findOneAndReplace(Bson filter, TDocument replacement, FindOneAndReplaceOptions options);
/**
* Atomically find a document and replace it.
*
* @param clientSession the client session with which to associate this operation
* @param filter the query filter to apply the the replace operation
* @param replacement the replacement document
* @param options the options to apply to the operation
* @return a publisher with a single element the document that was replaced. Depending on the value of the {@code returnOriginal}
* property, this will either be the document as it was before the update or as it is after the update. If no documents matched the
* query filter, then null will be returned
* @mongodb.server.release 3.6
* @since 1.7
*/
Publisher<TDocument> findOneAndReplace(ClientSession clientSession, Bson filter, TDocument replacement,
FindOneAndReplaceOptions options);