下面列出了怎么用com.mongodb.client.model.FindOneAndDeleteOptions的API类实例代码及写法,或者点击链接到github查看源代码。
@Test
void findAndDelete() {
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 frogman = collection.findOneAndDelete(new Document("id", 3)).await().indefinitely();
Document superman = collection
.findOneAndDelete(new Document("id", 1), new FindOneAndDeleteOptions().sort(new Document("id", 1)))
.await().indefinitely();
assertThat(frogman).contains(entry("stars", 1), entry("name", "frogman"));
assertThat(superman).contains(entry("stars", 5), entry("name", "superman"));
}
@Override
public Observable<TDocument> findOneAndDelete(final Bson filter, final FindOneAndDeleteOptions options) {
return RxObservables.create(Observables.observe(new Block<SingleResultCallback<TDocument>>() {
@Override
public void apply(final SingleResultCallback<TDocument> callback) {
wrapped.findOneAndDelete(filter, options, callback);
}
}), observableAdapter);
}
@Override
public Publisher<TDocument> findOneAndDelete(final Bson filter, final FindOneAndDeleteOptions 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.findOneAndDelete(filter, options, callback);
}
}));
}
@Override
public Publisher<TDocument> findOneAndDelete(final ClientSession clientSession, final Bson filter,
final FindOneAndDeleteOptions 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.findOneAndDelete(clientSession.getWrapped(), filter, options, callback);
}
}));
}
protected final FluentFuture<Optional<T>> doFindOneAndDelete(
final Constraints.ConstraintHost criteria,
final FindOneAndDeleteOptions options) {
checkNotNull(criteria, "criteria");
checkNotNull(options, "options");
return submit(new Callable<Optional<T>>() {
@Override
public Optional<T> call() throws Exception {
@Nullable T result = collection().findOneAndDelete(convertToBson(criteria), options);
return Optional.fromNullable(result);
}
});
}
/**
* Deletes and returns first matching document. Returns {@link Optional#absent()} if none
* documents matches.
* @return future of optional matching deleted document.
*/
public FluentFuture<Optional<T>> deleteFirst() {
checkState(numberToSkip == 0, "Cannot use .skip() with .deleteFirst()");
FindOneAndDeleteOptions options = new FindOneAndDeleteOptions();
options.sort(convertToBson(ordering));
return repository.doFindOneAndDelete(criteria, options);
}
@Override
public Uni<T> findOneAndDelete(Bson filter, FindOneAndDeleteOptions options) {
return Wrappers.toUni(collection.findOneAndDelete(filter, options));
}
@Override
public Uni<T> findOneAndDelete(ClientSession clientSession, Bson filter, FindOneAndDeleteOptions options) {
return Wrappers.toUni(collection.findOneAndDelete(clientSession, filter, options));
}
@Override
public Observable<TDocument> findOneAndDelete(final Bson filter) {
return findOneAndDelete(filter, new FindOneAndDeleteOptions());
}
@Override
public Publisher<TDocument> findOneAndDelete(final Bson filter) {
return findOneAndDelete(filter, new FindOneAndDeleteOptions());
}
@Override
public Publisher<TDocument> findOneAndDelete(final ClientSession clientSession, final Bson filter) {
return findOneAndDelete(clientSession, filter, new FindOneAndDeleteOptions());
}
@Test
public void findAndDeleteOptions() {
scan(FindOneAndDeleteOptions.class, FindAndDeleteOptions.class, true, List.of(WriteConcern.class));
}
/**
* Atomically find a document and remove it.
*
* @param filter the query filter to find the document with
* @param options the options to apply to the operation
* @return a {@link Uni} completed with the document that was removed. If no documents matched the query filter,
* then the uni is completed with {@code null}.
*/
Uni<T> findOneAndDelete(Bson filter, FindOneAndDeleteOptions options);
/**
* Atomically find a document and remove it.
*
* @param clientSession the client session with which to associate this operation
* @param filter the query filter to find the document with
* @param options the options to apply to the operation
* @return a {@link Uni} completed with the document that was removed. If no documents matched the query filter,
* then the uni is completed with {@code null}.
*/
Uni<T> findOneAndDelete(ClientSession clientSession, Bson filter, FindOneAndDeleteOptions options);
/**
* Atomically find a document and remove it.
*
* @param filter the query filter to find the document with
* @param options the options to apply to the operation
* @return an Observable with a single element the document that was removed. If no documents matched the query filter, then the
* observer will complete without emitting any items
*/
Observable<TDocument> findOneAndDelete(Bson filter, FindOneAndDeleteOptions options);
/**
* Atomically find a document and remove it.
*
* @param filter the query filter to find the document with
* @param options the options to apply to the operation
* @return a publisher with a single element the document that was removed. If no documents matched the query filter, then null will be
* returned
*/
Publisher<TDocument> findOneAndDelete(Bson filter, FindOneAndDeleteOptions options);
/**
* Atomically find a document and remove it.
*
* @param clientSession the client session with which to associate this operation
* @param filter the query filter to find the document with
* @param options the options to apply to the operation
* @return a publisher with a single element the document that was removed. If no documents matched the query filter, then null will be
* returned
* @mongodb.server.release 3.6
* @since 1.7
*/
Publisher<TDocument> findOneAndDelete(ClientSession clientSession, Bson filter, FindOneAndDeleteOptions options);