List of usage examples for com.mongodb.client.model FindOneAndReplaceOptions FindOneAndReplaceOptions
FindOneAndReplaceOptions
From source file:com.cognitive.cds.invocation.mongo.EngineInfoDao.java
License:Apache License
public boolean updateEngineInstanceState(EngineInstanceState engineInstanceState) throws JsonProcessingException { mongoDbDao.setDatabase("engine"); try {//ww w .j av a 2 s . c o m String json = mapper.writeValueAsString(engineInstanceState); logger.debug("=====> engineInstanceState json to write: " + json); Document filter = new Document(); filter.put("name", engineInstanceState.getName()); filter.put("type", engineInstanceState.getType()); filter.put("host", engineInstanceState.getHost()); filter.put("port", engineInstanceState.getPort()); Document doc = Document.parse(json); FindOneAndReplaceOptions options = new FindOneAndReplaceOptions(); options.upsert(true); doc.remove("_id"); mongoDbDao.getCollection("instance").findOneAndReplace(filter, doc, options); logger.debug("=====> engineInstanceState " + doc.toJson()); return true; } catch (Exception e) { logger.error( "=======> EngineInstanceState - Insert/Update Exception EngineInstanceState: " + e.toString()); return false; } }
From source file:io.lumeer.storage.mongodb.dao.collection.MongoDataDao.java
License:Open Source License
@Override public DataDocument updateData(final String collectionId, final String documentId, final DataDocument data) { Document document = new Document(data); FindOneAndReplaceOptions options = new FindOneAndReplaceOptions().returnDocument(ReturnDocument.AFTER); Document updatedDocument = dataCollection(collectionId).findOneAndReplace(idFilter(documentId), document, options);//from w ww .ja v a 2 s . c o m if (updatedDocument == null) { throw new StorageException("Document '" + documentId + "' has not been updated (replaced)."); } return MongoUtils.convertDocument(updatedDocument); }
From source file:io.lumeer.storage.mongodb.dao.organization.MongoCompanyContactDao.java
License:Open Source License
@Override public CompanyContact setCompanyContact(final Organization organization, final CompanyContact companyContact) { final FindOneAndReplaceOptions options = new FindOneAndReplaceOptions().upsert(true) .returnDocument(ReturnDocument.AFTER); try {//from w w w. jav a 2 s . c o m companyContact.setOrganizationId(organization.getId()); return databaseCollection().findOneAndReplace( MongoFilters.companyOrganizationIdFilter(organization.getId()), companyContact, options); } catch (MongoException ex) { throw new StorageException("Cannot update company contact " + companyContact, ex); } }
From source file:io.lumeer.storage.mongodb.dao.organization.MongoPaymentDao.java
License:Open Source License
@Override public Payment updatePayment(final Organization organization, final String id, final Payment payment) { FindOneAndReplaceOptions options = new FindOneAndReplaceOptions().returnDocument(ReturnDocument.AFTER) .upsert(true);//from w w w . j ava2 s .co m try { final Payment returnedPayment = databaseCollection(organization).findOneAndReplace(idFilter(id), payment, options); if (returnedPayment == null) { throw new StorageException("Payment '" + id + "' has not been updated."); } return returnedPayment; } catch (MongoException ex) { throw new StorageException("Cannot update payment " + payment, ex); } }
From source file:io.lumeer.storage.mongodb.dao.organization.MongoPaymentDao.java
License:Open Source License
@Override public Payment updatePayment(final Organization organization, final Payment payment) { FindOneAndReplaceOptions options = new FindOneAndReplaceOptions().returnDocument(ReturnDocument.AFTER) .upsert(true);//from ww w .j a v a2 s . c o m try { final Payment returnedPayment = databaseCollection(organization) .findOneAndReplace(paymentIdFilter(payment.getPaymentId()), payment, options); if (returnedPayment == null) { throw new StorageException("Payment '" + payment.getPaymentId() + "' has not been updated."); } return returnedPayment; } catch (MongoException ex) { throw new StorageException("Cannot update payment " + payment, ex); } }
From source file:io.lumeer.storage.mongodb.dao.project.MongoViewDao.java
License:Open Source License
@Override public View updateView(final String id, final View view) { JsonView jsonView = new JsonView(view); FindOneAndReplaceOptions options = new FindOneAndReplaceOptions().returnDocument(ReturnDocument.AFTER); try {// w w w .j av a2 s .co m View updatedView = databaseCollection().findOneAndReplace(idFilter(id), jsonView, options); if (updatedView == null) { throw new StorageException("View '" + view.getId() + "' has not been updated."); } return updatedView; } catch (MongoException ex) { throw new StorageException("Cannot update view: " + view, ex); } }
From source file:io.lumeer.storage.mongodb.dao.system.MongoGroupDao.java
License:Open Source License
@Override public Group updateGroup(final String id, final Group group) { FindOneAndReplaceOptions options = new FindOneAndReplaceOptions().returnDocument(ReturnDocument.AFTER) .upsert(true);//from www. jav a2s . co m try { Group returnedGroup = databaseCollection().findOneAndReplace(idFilter(id), group, options); if (returnedGroup == null) { throw new StorageException("Group '" + id + "' has not been updated."); } return returnedGroup; } catch (MongoException ex) { throw new StorageException("Cannot update group " + group, ex); } }
From source file:io.lumeer.storage.mongodb.dao.system.MongoUserDao.java
License:Open Source License
@Override public User updateUser(final String id, final User user) { FindOneAndReplaceOptions options = new FindOneAndReplaceOptions().returnDocument(ReturnDocument.AFTER) .upsert(true);/*from w w w .j a va2s.com*/ try { User returnedUser = databaseCollection().findOneAndReplace(idFilter(id), user, options); if (returnedUser == null) { throw new StorageException("User '" + id + "' has not been updated."); } return returnedUser; } catch (MongoException ex) { throw new StorageException("Cannot update user " + user, ex); } }
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);//from w w w.ja v a 2 s . co 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:my.beesyn.MongoConn.java
public void insert_or_update(Identifier idf, MongoCollection coll) { Document dc;// w w w. ja va 2 s. c o m FindOneAndReplaceOptions op = new FindOneAndReplaceOptions(); op.upsert(true); dc = Document.parse(idf.get_fetched()); dc.append("u_id", idf.get_id()); dc.append("chsum", dc.hashCode()); coll.findOneAndReplace(Filters.and(Filters.eq("u_id", idf.get_id()), Filters.ne("chsum", idf.get_chk())), dc, op); }