com.mongodb.client.model.FindOneAndReplaceOptions Java Examples

The following examples show how to use com.mongodb.client.model.FindOneAndReplaceOptions. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: Repositories.java    From immutables with Apache License 2.0 6 votes vote down vote up
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);
    }
  });

}
 
Example #2
Source File: AbstractMongoRepository.java    From edison-microservice with Apache License 2.0 6 votes vote down vote up
/**
 * 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");
    }
}
 
Example #3
Source File: Repositories.java    From immutables with Apache License 2.0 5 votes vote down vote up
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();
}
 
Example #4
Source File: MongoCollectionImpl.java    From mongo-java-driver-reactivestreams with Apache License 2.0 5 votes vote down vote up
@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);
                }
            }));
}
 
Example #5
Source File: MongoCollectionImpl.java    From mongo-java-driver-reactivestreams with Apache License 2.0 5 votes vote down vote up
@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);
                }
            }));
}
 
Example #6
Source File: MongoCollectionImpl.java    From mongo-java-driver-rx with Apache License 2.0 5 votes vote down vote up
@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);
}
 
Example #7
Source File: CollectionManagementTest.java    From quarkus with Apache License 2.0 5 votes vote down vote up
@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"));

}
 
Example #8
Source File: MongoCollectionImpl.java    From mongo-java-driver-rx with Apache License 2.0 4 votes vote down vote up
@Override
public Observable<TDocument> findOneAndReplace(final Bson filter, final TDocument replacement) {
    return findOneAndReplace(filter, replacement, new FindOneAndReplaceOptions());
}
 
Example #9
Source File: MongoCollectionImpl.java    From mongo-java-driver-reactivestreams with Apache License 2.0 4 votes vote down vote up
@Override
public Publisher<TDocument> findOneAndReplace(final Bson filter, final TDocument replacement) {
    return findOneAndReplace(filter, replacement, new FindOneAndReplaceOptions());
}
 
Example #10
Source File: ReactiveMongoCollectionImpl.java    From quarkus with Apache License 2.0 4 votes vote down vote up
@Override
public Uni<T> findOneAndReplace(ClientSession clientSession, Bson filter, T replacement,
        FindOneAndReplaceOptions options) {
    return Wrappers.toUni(collection.findOneAndReplace(clientSession, filter, replacement, options));
}
 
Example #11
Source File: MongoCollectionImpl.java    From mongo-java-driver-reactivestreams with Apache License 2.0 4 votes vote down vote up
@Override
public Publisher<TDocument> findOneAndReplace(final ClientSession clientSession, final Bson filter, final TDocument replacement) {
    return findOneAndReplace(clientSession, filter, replacement, new FindOneAndReplaceOptions());
}
 
Example #12
Source File: ReactiveMongoCollectionImpl.java    From quarkus with Apache License 2.0 4 votes vote down vote up
@Override
public Uni<T> findOneAndReplace(Bson filter, T replacement, FindOneAndReplaceOptions options) {
    return Wrappers.toUni(collection.findOneAndReplace(filter, replacement, options));
}
 
Example #13
Source File: MongoCollection.java    From mongo-java-driver-rx with Apache License 2.0 2 votes vote down vote up
/**
 * 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);
 
Example #14
Source File: MongoCollection.java    From mongo-java-driver-reactivestreams with Apache License 2.0 2 votes vote down vote up
/**
 * 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);
 
Example #15
Source File: MongoCollection.java    From mongo-java-driver-reactivestreams with Apache License 2.0 2 votes vote down vote up
/**
 * 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);
 
Example #16
Source File: ReactiveMongoCollection.java    From quarkus with Apache License 2.0 2 votes vote down vote up
/**
 * 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);
 
Example #17
Source File: ReactiveMongoCollection.java    From quarkus with Apache License 2.0 2 votes vote down vote up
/**
 * 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);