Example usage for com.mongodb.async.client MongoCollection findOneAndReplace

List of usage examples for com.mongodb.async.client MongoCollection findOneAndReplace

Introduction

In this page you can find the example usage for com.mongodb.async.client MongoCollection findOneAndReplace.

Prototype

void findOneAndReplace(ClientSession clientSession, Bson filter, TDocument replacement,
        SingleResultCallback<TDocument> callback);

Source Link

Document

Atomically find a document and replace it.

Usage

From source file:io.vertx.ext.mongo.impl.MongoClientImpl.java

License:Open Source License

@Override
public io.vertx.ext.mongo.MongoClient findOneAndReplaceWithOptions(String collection, JsonObject query,
        JsonObject replace, FindOptions findOptions, UpdateOptions updateOptions,
        Handler<AsyncResult<JsonObject>> resultHandler) {
    requireNonNull(collection, "collection cannot be null");
    requireNonNull(query, "query cannot be null");
    requireNonNull(findOptions, "find options cannot be null");
    requireNonNull(updateOptions, "update options cannot be null");
    requireNonNull(resultHandler, "resultHandler cannot be null");

    JsonObject encodedQuery = encodeKeyWhenUseObjectId(query);

    Bson bquery = wrap(encodedQuery);//  w ww.j  av a 2s  .  c o  m
    FindOneAndReplaceOptions foarOptions = new FindOneAndReplaceOptions();
    foarOptions.sort(wrap(findOptions.getSort()));
    foarOptions.projection(wrap(findOptions.getFields()));
    foarOptions.upsert(updateOptions.isUpsert());
    foarOptions.returnDocument(
            updateOptions.isReturningNewDocument() ? ReturnDocument.AFTER : ReturnDocument.BEFORE);

    MongoCollection<JsonObject> coll = getCollection(collection);
    coll.findOneAndReplace(bquery, replace, foarOptions, wrapCallback(resultHandler));
    return this;
}