List of usage examples for com.mongodb.client.model FindOneAndUpdateOptions returnDocument
ReturnDocument returnDocument
To view the source code for com.mongodb.client.model FindOneAndUpdateOptions returnDocument.
Click Source Link
From source file:com.avbravo.ejbjmoordb.mongodb.repository.Repository.java
/** * * @param key/*from w ww . 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 ww w.jav a 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(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/* www .j a v a 2 s. c o 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 av a 2 s . c om*/ * @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/*ww w . j a v a 2s.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/* w ww . j a v a 2 s .com*/ * @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 a v a 2 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.lumeer.storage.mongodb.MongoDbStorage.java
License:Open Source License
@Override public synchronized int getNextSequenceNo(final String collectionName, final String indexAttribute, final String index) { final FindOneAndUpdateOptions options = new FindOneAndUpdateOptions(); options.returnDocument(ReturnDocument.AFTER); final Document doc = database.getCollection(collectionName).findOneAndUpdate(eq(indexAttribute, index), inc("seq", 1), options); if (doc == null) { // the sequence did not exist resetSequence(collectionName, indexAttribute, index); return 0; } else {/*from w w w . j a v a2 s . c o m*/ return doc.getInteger("seq"); } }
From source file:io.lumeer.storage.mongodb.MongoDbStorage.java
License:Open Source License
@Override public synchronized void resetSequence(final String collectionName, final String indexAttribute, final String index) { final FindOneAndUpdateOptions options = new FindOneAndUpdateOptions(); options.returnDocument(ReturnDocument.AFTER); final Document doc = database.getCollection(collectionName).findOneAndUpdate(eq(indexAttribute, index), set("seq", 0), options); if (doc == null) { Document newSeq = new Document(); newSeq.put(indexAttribute, index); newSeq.put("seq", 0); database.getCollection(collectionName).insertOne(newSeq); }//from www.j a v a 2 s .c o m }
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 w w. j a v a 2 s . com*/ 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; }