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

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

Introduction

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

Prototype

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

Source Link

Document

Replace a document in the collection according to the specified arguments.

Usage

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

License:Open Source License

@Override
public io.vertx.ext.mongo.MongoClient saveWithOptions(String collection, JsonObject document,
        WriteOption writeOption, Handler<AsyncResult<String>> resultHandler) {
    requireNonNull(collection, "collection cannot be null");
    requireNonNull(document, "document cannot be null");
    requireNonNull(resultHandler, "resultHandler cannot be null");

    MongoCollection<JsonObject> coll = getCollection(collection, writeOption);
    Object id = document.getValue(ID_FIELD);
    if (id == null) {
        coll.insertOne(document,/* ww w. j ava2 s  .c om*/
                convertCallback(resultHandler,
                        wr -> useObjectId
                                ? document.getJsonObject(ID_FIELD).getString(JsonObjectCodec.OID_FIELD)
                                : document.getString(ID_FIELD)));
    } else {
        JsonObject filter = new JsonObject();
        JsonObject encodedDocument = encodeKeyWhenUseObjectId(document);
        filter.put(ID_FIELD, encodedDocument.getValue(ID_FIELD));

        com.mongodb.client.model.UpdateOptions updateOptions = new com.mongodb.client.model.UpdateOptions()
                .upsert(true);

        coll.replaceOne(wrap(filter), encodedDocument, updateOptions,
                convertCallback(resultHandler, result -> null));
    }
    return this;
}

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

License:Open Source License

@Override
public MongoClient replaceDocumentsWithOptions(String collection, JsonObject query, JsonObject replace,
        UpdateOptions options, Handler<AsyncResult<MongoClientUpdateResult>> resultHandler) {
    requireNonNull(collection, "collection cannot be null");
    requireNonNull(query, "query cannot be null");
    requireNonNull(replace, "update cannot be null");
    requireNonNull(options, "options cannot be null");
    requireNonNull(resultHandler, "resultHandler cannot be null");

    MongoCollection<JsonObject> coll = getCollection(collection, options.getWriteOption());
    Bson bquery = wrap(encodeKeyWhenUseObjectId(query));
    coll.replaceOne(bquery, encodeKeyWhenUseObjectId(replace), mongoUpdateOptions(options),
            toMongoClientUpdateResult(resultHandler));
    return this;
}