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

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

Introduction

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

Prototype

ReturnDocument AFTER

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

Click Source Link

Document

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

Usage

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

/**
 *
 * @param key/* 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  . j  a v  a 2  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(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  a v a 2  s. 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);

        } 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//  ww w  .  ja  v a2 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(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 www  .  j  a va  2  s .c om*/
 *
 * @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  . j av  a2 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);
            //                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  w w.  j av a2 s . co  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: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();//w  w w .  j  a  va  2s . c o  m
    })).subscribeOn(Schedulers.io()).cast(Document.class);
}

From source file:com.exorath.service.currency.service.MongoService.java

License:Apache License

@Override
public IncrementSuccess increment(IncrementReq req) {
    Document filter = getFilter(req.getCurrency(), req.getUuid());
    Document result;/* w  w w. jav  a2  s.  c o m*/
    if (req.getMin() != null) {

        filter.append("balance", new Document("$gte", req.getMin()));
        result = accounts.findOneAndUpdate(filter, Updates.inc("balance", req.getAmount()),
                new FindOneAndUpdateOptions().returnDocument(ReturnDocument.AFTER));
        if (result == null)
            return new IncrementSuccess(1, "Insufficient funds");
    } else
        result = accounts.findOneAndUpdate(filter, Updates.inc("balance", req.getAmount()),
                new FindOneAndUpdateOptions().returnDocument(ReturnDocument.AFTER).upsert(true));
    if (result == null)
        return new IncrementSuccess(-1, "Unknown error");
    return new IncrementSuccess(result.getInteger("balance"));
}

From source file:com.ibm.research.mongotx.lrc.LatestReadCommittedTxDB.java

License:Open Source License

public long incrementAndGetLong(Object key) {
    return (long) sysCol
            .findOneAndUpdate(new Document(ATTR_ID, key), UPDATE_SEQ_INCREAMENT,
                    new FindOneAndUpdateOptions().returnDocument(ReturnDocument.AFTER).upsert(true))
            .get(ATTR_SEQ);/*from www  . j a v a 2s .c  om*/
}