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

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

Introduction

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

Prototype

FindOneAndUpdateOptions

Source Link

Usage

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

/**
 *
 * @param key//from  w w w. j a  v  a  2  s . co 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 w w.  ja  v  a2s  .  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(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   w  w w  .j  a  va2 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  ww w.j av  a 2 s. com*/
 * @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//w w w  .j av  a2 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(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  va 2s. c om
 * @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 .  j a  v  a2s . com*/

        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:com.creactiviti.piper.core.task.MongoCounterRepository.java

License:Apache License

@Override
public void set(String aCounterName, long aValue) {
    collection//from w  w  w .  j  a  v  a  2  s . c  om
            .findOneAndUpdate(eq("_id", aCounterName),
                    combine(Updates.set(DSL.VALUE, aValue), setOnInsert("_id", aCounterName),
                            setOnInsert(DSL.CREATE_TIME, new Date())),
                    new FindOneAndUpdateOptions().upsert(true));
}

From source file:com.creactiviti.piper.core.task.MongoCounterRepository.java

License:Apache License

@Override
public long decrement(String aCounterName) {
    Document doc = collection.findOneAndUpdate(eq("_id", aCounterName), inc(DSL.VALUE, -1),
            new FindOneAndUpdateOptions().returnDocument(AFTER).projection(include(DSL.VALUE)));

    if (doc == null) {
        throw new IllegalArgumentException("Counter not found: " + aCounterName);
    }//from  www.ja  v  a  2s . co  m

    return doc.getLong(DSL.VALUE);
}

From source file:com.exorath.exodata.impl.IExoDocument.java

License:Apache License

@Override
public Observable<Document> fetch(Bson projection) {

    return Observable.create((subscriber -> {
        FindOneAndUpdateOptions options = new FindOneAndUpdateOptions().upsert(true)
                .returnDocument(ReturnDocument.AFTER).projection(projection);
        document = (Document) collection.findOneAndUpdate(getIdQuery(),
                Updates.setOnInsert("_id", getId().toString()), options);
        if (document != null)
            subscriber.onNext(document);
        subscriber.onCompleted();//from   www  .  j a va  2  s . co m
    })).subscribeOn(Schedulers.io()).cast(Document.class);
}