Example usage for com.mongodb.client.model FindOneAndUpdateOptions upsert

List of usage examples for com.mongodb.client.model FindOneAndUpdateOptions upsert

Introduction

In this page you can find the example usage for com.mongodb.client.model FindOneAndUpdateOptions upsert.

Prototype

boolean upsert

To view the source code for com.mongodb.client.model FindOneAndUpdateOptions upsert.

Click Source Link

Usage

From source file:com.avbravo.ejbjmoordb.mongodb.repository.Repository.java

/**
 *
 * @param key/* w  w  w  . j a v  a2  s  .  c  o  m*/
 * @param value
 * @param field
 * @return
 */
public T findOneAndUpdate(String key, String value, String field, Integer... incremento) {

    try {
        Integer increment = 1;
        if (incremento.length != 0) {
            increment = incremento[0];

        }
        Document doc = new Document(key, value);
        Document inc = new Document("$inc", new Document(field, increment));

        FindOneAndUpdateOptions findOneAndUpdateOptions = new FindOneAndUpdateOptions();
        findOneAndUpdateOptions.upsert(true);

        findOneAndUpdateOptions.returnDocument(ReturnDocument.AFTER);

        Object t = entityClass.newInstance();

        MongoDatabase db = getMongoClient().getDatabase(database);
        Document iterable = db.getCollection(collection).findOneAndUpdate(doc, inc, findOneAndUpdateOptions);

        try {

            t1 = (T) documentToJava.fromDocument(entityClass, iterable, embeddedBeansList, referencedBeansList);

        } catch (Exception e) {
            Logger.getLogger(Repository.class.getName() + "findOneAndUpdate()").log(Level.SEVERE, null, e);
            exception = new Exception("findOneAndUpdate()", e);
        }

    } catch (Exception e) {
        Logger.getLogger(Repository.class.getName()).log(Level.SEVERE, null, e);
        exception = new Exception("findOneAndUpdate()", e);
    }

    return t1;
}

From source file:com.avbravo.ejbjmoordb.mongodb.repository.Repository.java

/**
 * findOneAndUpdate//from  w ww .jav  a 2 s .  co  m
 *
 * @param doc
 * @param field
 * @param incremento
 * @return
 */
public T findOneAndUpdate(Document doc, String field, Integer... incremento) {
    try {
        Integer increment = 1;
        if (incremento.length != 0) {
            increment = incremento[0];

        }

        Document inc = new Document("$inc", new Document(field, increment));

        FindOneAndUpdateOptions findOneAndUpdateOptions = new FindOneAndUpdateOptions();
        findOneAndUpdateOptions.upsert(true);

        findOneAndUpdateOptions.returnDocument(ReturnDocument.AFTER);

        Object t = entityClass.newInstance();
        list = new ArrayList<>();

        MongoDatabase db = getMongoClient().getDatabase(database);
        Document iterable = db.getCollection(collection).findOneAndUpdate(doc, inc, findOneAndUpdateOptions);

        try {
            t1 = (T) documentToJava.fromDocument(entityClass, iterable, embeddedBeansList, referencedBeansList);
            //                Method method = entityClass.getDeclaredMethod("toPojo", Document.class);
            //                list.add((T) method.invoke(t, iterable));
        } catch (Exception e) {
            Logger.getLogger(Repository.class.getName() + "findOneAndUpdate()").log(Level.SEVERE, null, e);
            exception = new Exception("findOneAndUpdate()", e);
        }

    } catch (Exception e) {
        Logger.getLogger(Repository.class.getName()).log(Level.SEVERE, null, e);
        exception = new Exception("findOneAndUpdate()", e);
    }

    return t1;
}

From source file:com.avbravo.ejbjmoordb.mongodb.repository.Repository.java

/**
 *
 * @param doc//from  ww  w  .  j  av  a 2  s.  co m
 * @param inc
 * @param incremento
 * @return
 */
public T findOneAndUpdate(Document doc, Document inc, Integer... incremento) {
    try {

        FindOneAndUpdateOptions findOneAndUpdateOptions = new FindOneAndUpdateOptions();
        findOneAndUpdateOptions.upsert(true);

        findOneAndUpdateOptions.returnDocument(ReturnDocument.AFTER);

        Object t = entityClass.newInstance();
        list = new ArrayList<>();

        MongoDatabase db = getMongoClient().getDatabase(database);
        Document iterable = db.getCollection(collection).findOneAndUpdate(doc, inc, findOneAndUpdateOptions);

        try {
            t1 = (T) documentToJava.fromDocument(entityClass, iterable, embeddedBeansList, referencedBeansList);

        } catch (Exception e) {
            Logger.getLogger(Repository.class.getName() + "findOneAndUpdate()").log(Level.SEVERE, null, e);
            exception = new Exception("findOneAndUpdate()", e);
        }

    } catch (Exception e) {
        Logger.getLogger(Repository.class.getName()).log(Level.SEVERE, null, e);
        exception = new Exception("findOneAndUpdate()", e);
    }

    return t1;
}

From source file:com.avbravo.jmoordbdianna.mongodb.facade.AbstractFacade.java

/**
 *
 * @param key//from w w w  . j a  va  2 s. c o m
 * @param value
 * @param field
 * @return
 */
public T findOneAndUpdate(String key, String value, String field, Integer... incremento) {

    try {
        Integer increment = 1;
        if (incremento.length != 0) {
            increment = incremento[0];

        }
        Document doc = new Document(key, value);
        Document inc = new Document("$inc", new Document(field, increment));

        FindOneAndUpdateOptions findOneAndUpdateOptions = new FindOneAndUpdateOptions();
        findOneAndUpdateOptions.upsert(true);

        findOneAndUpdateOptions.returnDocument(ReturnDocument.AFTER);

        Object t = entityClass.newInstance();

        MongoDatabase db = getMongoClient().getDatabase(database);
        Document iterable = db.getCollection(collection).findOneAndUpdate(doc, inc, findOneAndUpdateOptions);

        try {

            t1 = (T) documentToJava.fromDocument(entityClass, iterable, embeddedBeansList, referencedBeansList);

        } catch (Exception e) {
            Logger.getLogger(AbstractFacade.class.getName() + "findOneAndUpdate()").log(Level.SEVERE, null, e);
            exception = new Exception("findOneAndUpdate()", e);
        }

    } catch (Exception e) {
        Logger.getLogger(AbstractFacade.class.getName()).log(Level.SEVERE, null, e);
        exception = new Exception("findOneAndUpdate()", e);
    }

    return t1;
}

From source file:com.avbravo.jmoordbdianna.mongodb.facade.AbstractFacade.java

/**
 * findOneAndUpdate/*from   w  ww  .  j  a  v  a2 s .  c o  m*/
 *
 * @param doc
 * @param field
 * @param incremento
 * @return
 */
public T findOneAndUpdate(Document doc, String field, Integer... incremento) {
    try {
        Integer increment = 1;
        if (incremento.length != 0) {
            increment = incremento[0];

        }

        Document inc = new Document("$inc", new Document(field, increment));

        FindOneAndUpdateOptions findOneAndUpdateOptions = new FindOneAndUpdateOptions();
        findOneAndUpdateOptions.upsert(true);

        findOneAndUpdateOptions.returnDocument(ReturnDocument.AFTER);

        Object t = entityClass.newInstance();
        list = new ArrayList<>();

        MongoDatabase db = getMongoClient().getDatabase(database);
        Document iterable = db.getCollection(collection).findOneAndUpdate(doc, inc, findOneAndUpdateOptions);

        try {
            t1 = (T) documentToJava.fromDocument(entityClass, iterable, embeddedBeansList, referencedBeansList);
            //                Method method = entityClass.getDeclaredMethod("toPojo", Document.class);
            //                list.add((T) method.invoke(t, iterable));
        } catch (Exception e) {
            Logger.getLogger(AbstractFacade.class.getName() + "findOneAndUpdate()").log(Level.SEVERE, null, e);
            exception = new Exception("findOneAndUpdate()", e);
        }

    } catch (Exception e) {
        Logger.getLogger(AbstractFacade.class.getName()).log(Level.SEVERE, null, e);
        exception = new Exception("findOneAndUpdate()", e);
    }

    return t1;
}

From source file:com.avbravo.jmoordbdianna.mongodb.facade.AbstractFacade.java

/**
 *
 * @param doc//from  w w  w .  ja  v  a 2s.co m
 * @param inc
 * @param incremento
 * @return
 */
public T findOneAndUpdate(Document doc, Document inc, Integer... incremento) {
    try {

        FindOneAndUpdateOptions findOneAndUpdateOptions = new FindOneAndUpdateOptions();
        findOneAndUpdateOptions.upsert(true);

        findOneAndUpdateOptions.returnDocument(ReturnDocument.AFTER);

        Object t = entityClass.newInstance();
        list = new ArrayList<>();

        MongoDatabase db = getMongoClient().getDatabase(database);
        Document iterable = db.getCollection(collection).findOneAndUpdate(doc, inc, findOneAndUpdateOptions);

        try {
            t1 = (T) documentToJava.fromDocument(entityClass, iterable, embeddedBeansList, referencedBeansList);
            //                Method method = entityClass.getDeclaredMethod("toPojo", Document.class);
            //                list.add((T) method.invoke(t, iterable));
        } catch (Exception e) {
            Logger.getLogger(AbstractFacade.class.getName() + "findOneAndUpdate()").log(Level.SEVERE, null, e);
            exception = new Exception("findOneAndUpdate()", e);
        }

    } catch (Exception e) {
        Logger.getLogger(AbstractFacade.class.getName()).log(Level.SEVERE, null, e);
        exception = new Exception("findOneAndUpdate()", e);
    }

    return t1;
}

From source file:com.bluedragon.mongo.MongoCollectionFindAndModify.java

License:Open Source License

@SuppressWarnings("rawtypes")
public cfData execute(cfSession _session, cfArgStructData argStruct) throws cfmRunTimeException {
    MongoDatabase db = getMongoDatabase(_session, argStruct);

    String collection = getNamedStringParam(argStruct, "collection", null);
    if (collection == null)
        throwException(_session, "please specify a collection");

    cfData update = getNamedParam(argStruct, "update", null);
    if (update == null)
        throwException(_session, "please specify update");

    cfData query = getNamedParam(argStruct, "query", null);
    if (query == null)
        throwException(_session, "please specify query to update");

    try {/*  w  ww  .jav  a2  s  .c  o m*/

        MongoCollection<Document> col = db.getCollection(collection);
        FindOneAndUpdateOptions findOneAndUpdateOptions = new FindOneAndUpdateOptions();

        if (getNamedParam(argStruct, "fields", null) != null)
            findOneAndUpdateOptions.projection(getDocument(getNamedParam(argStruct, "fields", null)));

        if (getNamedParam(argStruct, "sort", null) != null)
            findOneAndUpdateOptions.sort(getDocument(getNamedParam(argStruct, "sort", null)));

        findOneAndUpdateOptions.upsert(getNamedBooleanParam(argStruct, "upsert", false));

        if (getNamedBooleanParam(argStruct, "returnnew", false))
            findOneAndUpdateOptions.returnDocument(ReturnDocument.AFTER);

        Document qry = getDocument(query);
        long start = System.currentTimeMillis();

        Document result = col.findOneAndUpdate(qry, getDocument(update), findOneAndUpdateOptions);

        _session.getDebugRecorder().execMongo(col, "findandmodify", qry, System.currentTimeMillis() - start);

        return tagUtils.convertToCfData((Map) result);

    } catch (MongoException me) {
        throwException(_session, me.getMessage());
        return null;
    }
}

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