Example usage for com.mongodb.client.model ReturnDocument BEFORE

List of usage examples for com.mongodb.client.model ReturnDocument BEFORE

Introduction

In this page you can find the example usage for com.mongodb.client.model ReturnDocument BEFORE.

Prototype

ReturnDocument BEFORE

To view the source code for com.mongodb.client.model ReturnDocument BEFORE.

Click Source Link

Document

Indicates to return the document before the update, replacement, or insert occurred.

Usage

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

License:Open Source License

@Override
public io.vertx.ext.mongo.MongoClient findOneAndUpdateWithOptions(String collection, JsonObject query,
        JsonObject update, 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);/*from ww  w .j  av  a 2  s  .  co m*/
    Bson bupdate = wrap(update);
    FindOneAndUpdateOptions foauOptions = new FindOneAndUpdateOptions();
    foauOptions.sort(wrap(findOptions.getSort()));
    foauOptions.projection(wrap(findOptions.getFields()));
    foauOptions.upsert(updateOptions.isUpsert());
    foauOptions.returnDocument(
            updateOptions.isReturningNewDocument() ? ReturnDocument.AFTER : ReturnDocument.BEFORE);

    MongoCollection<JsonObject> coll = getCollection(collection);
    coll.findOneAndUpdate(bquery, bupdate, foauOptions, wrapCallback(resultHandler));
    return this;
}

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 w  w  .ja v  a 2  s .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;
}

From source file:org.mongojack.JacksonMongoCollection.java

License:Apache License

/**
 * Finds the first document in the query and updates it.
 * //from   w w w. j  a  v  a 2s . c om
 * @param query
 *            query to match
 * @param fields
 *            fields to be returned
 * @param sort
 *            sort to apply before picking first document
 * @param update
 *            update to apply. This must contain only update operators
 * @param returnNew
 *            if true, the updated document is returned, otherwise the old
 *            document is returned (or it would be lost forever)
 * @param upsert
 *            do upsert (insert if document not present)
 * @return the object
 */
public T findAndModify(Document query, Document fields, Document sort, Document update, boolean returnNew,
        boolean upsert) {
    return mongoCollection.findOneAndUpdate(serializeFields(query), update,
            new FindOneAndUpdateOptions()
                    .returnDocument(returnNew ? ReturnDocument.AFTER : ReturnDocument.BEFORE).projection(fields)
                    .sort(sort).upsert(upsert));
}

From source file:org.mongojack.JacksonMongoCollection.java

License:Apache License

/**
 * Finds the first document in the query and updates it.
 * /*from  www . ja v a  2 s. c o  m*/
 * @param query
 *            query to match
 * @param fields
 *            fields to be returned
 * @param sort
 *            sort to apply before picking first document
 * @param update
 *            update to apply
 * @param returnNew
 *            if true, the updated document is returned, otherwise the old
 *            document is returned (or it would be lost forever)
 * @param upsert
 *            do upsert (insert if document not present)
 * @return the object
 */
public T findAndModify(DBQuery.Query query, Document fields, Document sort, DBUpdate.Builder update,
        boolean returnNew, boolean upsert) {
    return mongoCollection.findOneAndUpdate(serializeQuery(query),
            update.serializeAndGetAsDocument(objectMapper, type),
            new FindOneAndUpdateOptions()
                    .returnDocument(returnNew ? ReturnDocument.AFTER : ReturnDocument.BEFORE).projection(fields)
                    .sort(sort).upsert(upsert));
}

From source file:org.mongojack.JacksonMongoCollection.java

License:Apache License

/**
 * Finds the first document in the query and updates it.
 * /*from   w w w .j a va  2 s  .  co  m*/
 * @param query
 *            query to match
 * @param fields
 *            fields to be returned
 * @param sort
 *            sort to apply before picking first document
 * @param update
 *            update to apply
 * @param returnNew
 *            if true, the updated document is returned, otherwise the old
 *            document is returned (or it would be lost forever)
 * @param upsert
 *            do upsert (insert if document not present)
 * @return the object
 */
public T findAndModify(Document query, Document fields, Document sort, DBUpdate.Builder update,
        boolean returnNew, boolean upsert) {
    return mongoCollection.findOneAndUpdate(serializeFields(query),
            update.serializeAndGetAsDocument(objectMapper, type),
            new FindOneAndUpdateOptions()
                    .returnDocument(returnNew ? ReturnDocument.AFTER : ReturnDocument.BEFORE).projection(fields)
                    .sort(sort).upsert(upsert));
}

From source file:org.opencb.commons.datastore.mongodb.MongoDBNativeQuery.java

License:Apache License

public Document findAndUpdate(Bson query, Bson projection, Bson sort, Bson update, QueryOptions options) {
    boolean upsert = false;
    boolean returnNew = false;

    if (options != null) {
        if (projection == null) {
            projection = getProjection(projection, options);
        }/*from w w  w .  j a  va  2s .  com*/
        upsert = options.getBoolean("upsert", false);
        returnNew = options.getBoolean("returnNew", false);
    }

    FindOneAndUpdateOptions findOneAndUpdateOptions = new FindOneAndUpdateOptions().sort(sort)
            .projection(projection).upsert(upsert)
            .returnDocument(returnNew ? ReturnDocument.AFTER : ReturnDocument.BEFORE);
    return dbCollection.findOneAndUpdate(query, update, findOneAndUpdateOptions);
}

From source file:org.opencb.commons.datastore.mongodb.MongoDBNativeQuery.java

License:Apache License

public Document findAndModify(Bson query, Bson projection, Bson sort, Document update, QueryOptions options) {
    boolean remove = false;
    boolean upsert = false;
    boolean returnNew = false;

    if (options != null) {
        if (projection == null) {
            projection = getProjection(projection, options);
        }//from  w  w w . jav  a2s  .  c  om
        remove = options.getBoolean("remove", false);
        upsert = options.getBoolean("upsert", false);
        returnNew = options.getBoolean("returnNew", false);
    }

    if (remove) {
        FindOneAndReplaceOptions findOneAndReplaceOptions = new FindOneAndReplaceOptions().sort(sort)
                .projection(projection).upsert(upsert)
                .returnDocument(returnNew ? ReturnDocument.AFTER : ReturnDocument.BEFORE);
        return dbCollection.findOneAndReplace(query, update, findOneAndReplaceOptions);
    } else {
        FindOneAndUpdateOptions findOneAndUpdateOptions = new FindOneAndUpdateOptions().sort(sort)
                .projection(projection).upsert(upsert)
                .returnDocument(returnNew ? ReturnDocument.AFTER : ReturnDocument.BEFORE);
        return dbCollection.findOneAndUpdate(query, update, findOneAndUpdateOptions);
    }
    //        return dbCollection.findOneAndUpdate(query, projection, sort, remove, update, returnNew, upsert);
}

From source file:rapture.repo.mongodb.MongoDbDataStore.java

License:Open Source License

@Override
public void put(String key, String value) {
    MongoCollection<Document> collection = getCollection();
    Document query = new Document(KEY, key);
    Document toPut = new Document($SET, new Document(KEY, key).append(VALUE, value));

    FindOneAndUpdateOptions options = new FindOneAndUpdateOptions().upsert(true)
            .returnDocument(ReturnDocument.BEFORE);
    Document result = collection.findOneAndUpdate(query, toPut, options);

    if (needsFolderHandling && result == null) {
        dirRepo.registerParentage(key);//ww  w  . j  a va2  s.co  m
    }
}