@Signature public void updateOne(BasicDBObject filter, BasicDBObject update) { getWrappedObject().updateOne(filter, update); }
UpdateResult result = collection.updateOne(query, update); if (result.wasAcknowledged() && result.getMatchedCount() == 0) { System.err.println("Nothing updated for key " + key);
/** * Update a single or all documents in the collection according to the specified arguments. * When upsert set to true, the new document will be inserted if there are no matches to the query filter. * * @param filter Bson filter * @param document Bson document * @param upsert a new document should be inserted if there are no matches to the query filter * @param many whether find all documents according to the query filter */ public void update(Bson filter, Bson document, boolean upsert, boolean many) { //TODO batch updating UpdateOptions options = new UpdateOptions(); if (upsert) { options.upsert(true); } if (many) { collection.updateMany(filter, document, options); } else { collection.updateOne(filter, document, options); } }
private boolean takeOverTransactionInMongoDB(TransactionXid transactionXid, String source, String target) { byte[] global = transactionXid.getGlobalTransactionId(); String instanceId = ByteUtils.byteArrayToString(global); try { String application = CommonUtils.getApplication(this.endpoint); String databaseName = application.replaceAll("\\W", "_"); MongoDatabase mdb = this.mongoClient.getDatabase(databaseName); MongoCollection<Document> collection = mdb.getCollection(CONSTANTS_TB_LOCKS); Bson globalFilter = Filters.eq(CONSTANTS_FD_GLOBAL, instanceId); Bson instIdFilter = Filters.eq("identifier", source); Document document = new Document("$set", new Document("identifier", target)); UpdateResult result = collection.updateOne(Filters.and(globalFilter, instIdFilter), document); return result.getMatchedCount() == 1; } catch (RuntimeException rex) { logger.error("Error occurred while locking transaction(gxid= {}).", instanceId, rex); return false; } }
private void markTransactionRollback(TransactionXid transactionXid) { try { byte[] global = transactionXid.getGlobalTransactionId(); String identifier = ByteUtils.byteArrayToString(global); String application = CommonUtils.getApplication(this.endpoint); String databaseName = application.replaceAll("\\W", "_"); MongoDatabase mdb = this.mongoClient.getDatabase(databaseName); MongoCollection<Document> collection = mdb.getCollection(CONSTANTS_TB_TRANSACTIONS); Document document = new Document(); document.append("$set", new Document("status", Status.STATUS_MARKED_ROLLBACK)); Bson globalFilter = Filters.eq(CONSTANTS_FD_GLOBAL, identifier); Bson statusFilter = Filters.eq("status", Status.STATUS_ACTIVE); collection.updateOne(Filters.and(globalFilter, statusFilter), document); } catch (RuntimeException error) { logger.error("Error occurred while setting the error flag.", error); } }
private Consumer<MongoClient> createUpdateOneItem(int id, String document) { return client -> client.getDatabase(DB_NAME).getCollection(this.getCollectionName()) .updateOne(Document.parse(String.format("{'_id' : %d}", id)), Document.parse(document)); } }
final UpdateResult result = collection.updateOne(eq(FIELD_ALERT_CONDITIONS_ID, alertConditionId), set(ALERT_CONDITIONS_PARAMETERS_PREFIX + field, intValue));
@Override public void save(final Session session) { syncTtl(); String id = session.id(); Bson filter = Filters.eq("_id", id); Document doc = new Document() .append("_id", id) .append("_accessedAt", new Date(session.accessedAt())) .append("_createdAt", new Date(session.createdAt())) .append("_savedAt", new Date(session.savedAt())); // dump attributes Map<String, String> attributes = session.attributes(); attributes.forEach((k, v) -> doc.append(encode(k), v)); sessions.updateOne(filter, new Document("$set", doc), new UpdateOptions().upsert(true)); }
public void deleteParticipant(XAResourceArchive archive) { try { TransactionXid transactionXid = (TransactionXid) archive.getXid(); byte[] global = transactionXid.getGlobalTransactionId(); byte[] branch = transactionXid.getBranchQualifier(); String globalKey = ByteUtils.byteArrayToString(global); String branchKey = ByteUtils.byteArrayToString(branch); String application = CommonUtils.getApplication(this.endpoint); String databaseName = application.replaceAll("\\W", "_"); MongoDatabase mdb = this.mongoClient.getDatabase(databaseName); MongoCollection<Document> collection = mdb.getCollection(CONSTANTS_TB_TRANSACTIONS); Document participants = new Document(); participants.append(String.format("participants.%s", branchKey), null); Document document = new Document(); document.append("$unset", participants); UpdateResult result = collection.updateOne(Filters.eq(CONSTANTS_FD_GLOBAL, globalKey), document); if (result.getMatchedCount() != 1) { throw new IllegalStateException( String.format("Error occurred while deleting participant(matched= %s, modified= %s).", result.getMatchedCount(), result.getModifiedCount())); } } catch (RuntimeException error) { logger.error("Error occurred while deleting participant.", error); this.beanFactory.getCompensableManager().setRollbackOnlyQuietly(); } }
@Override public void upgrade() { if (clusterConfigService.get(MigrationCompleted.class) != null) { LOG.debug("Migration already done."); return; } // Do not overwrite an existing default index config boolean defaultDone = clusterConfigService.get(DefaultIndexSetConfig.class) != null; final ImmutableSet.Builder<String> builder = ImmutableSet.builder(); final FindIterable<Document> documents = collection.find(exists(FIELD_DEFAULT)).sort(ascending(FIELD_CREATION_DATE)); for (final Document document : documents) { final ObjectId id = document.getObjectId(FIELD_ID); final String idString = id.toHexString(); final boolean isDefault = firstNonNull(document.getBoolean(FIELD_DEFAULT), false); if (!defaultDone && isDefault) { defaultDone = true; clusterConfigService.write(DefaultIndexSetConfig.create(idString)); } final long modifiedCount = collection.updateOne(eq(FIELD_ID, id), unset(FIELD_DEFAULT)).getMatchedCount(); if (modifiedCount > 0) { LOG.info("Removed <default> field from index set <{}> ({})", document.getString(FIELD_TITLE), idString); builder.add(idString); } else { LOG.error("Couldn't remove <default> field from index set <{}> ({})", document.getString(FIELD_TITLE), idString); } } clusterConfigService.write(MigrationCompleted.create(builder.build())); }
public void putErrorTransaction(TransactionXid transactionXid, Transaction transaction) { try { TransactionArchive archive = (TransactionArchive) transaction.getTransactionArchive(); byte[] global = transactionXid.getGlobalTransactionId(); String identifier = ByteUtils.byteArrayToString(global); int status = archive.getCompensableStatus(); String databaseName = CommonUtils.getApplication(this.endpoint).replaceAll("\\W", "_"); MongoDatabase mdb = this.mongoClient.getDatabase(databaseName); MongoCollection<Document> collection = mdb.getCollection(CONSTANTS_TB_TRANSACTIONS); Document target = new Document(); target.append("modified", this.endpoint); target.append("status", status); target.append("error", true); target.append("recovered_at", archive.getRecoveredAt() == 0 ? null : new Date(archive.getRecoveredAt())); target.append("recovered_times", archive.getRecoveredTimes()); Document document = new Document(); document.append("$set", target); // document.append("$inc", new BasicDBObject("modified_time", 1)); UpdateResult result = collection.updateOne(Filters.eq(CONSTANTS_FD_GLOBAL, identifier), document); if (result.getMatchedCount() != 1) { throw new IllegalStateException( String.format("Error occurred while updating transaction(matched= %s, modified= %s).", result.getMatchedCount(), result.getModifiedCount())); } } catch (RuntimeException error) { logger.error("Error occurred while setting the error flag.", error); } }
private void executeRename(@Nullable final ClientSession clientSession, final BsonValue id, final String newFilename) { UpdateResult updateResult; if (clientSession != null) { updateResult = filesCollection.updateOne(clientSession, new BsonDocument("_id", id), new BsonDocument("$set", new BsonDocument("filename", new BsonString(newFilename)))); } else { updateResult = filesCollection.updateOne(new BsonDocument("_id", id), new BsonDocument("$set", new BsonDocument("filename", new BsonString(newFilename)))); } if (updateResult.wasAcknowledged() && updateResult.getMatchedCount() == 0) { throw new MongoGridFSException(format("No file found with the id: %s", id)); } }
UpdateResult result = collection.updateOne(Filters.eq(CONSTANTS_FD_GLOBAL, identifier), document); if (result.getMatchedCount() != 1) { throw new IllegalStateException(
document.append("$set", participants); UpdateResult result = collection.updateOne(Filters.eq(CONSTANTS_FD_GLOBAL, globalKey), document); if (result.getMatchedCount() != 1) { throw new IllegalStateException(
return collection.updateMany(queryObj, updateObj, opts); } else { return collection.updateOne(queryObj, updateObj, opts);
document.append("$set", compensables); UpdateResult result = collection.updateOne(Filters.eq(CONSTANTS_FD_GLOBAL, globalKey), document); if (result.getMatchedCount() != 1) { throw new IllegalStateException(
JsonNode json = new ObjectMapper().readTree(body); JsonNode jsonEncoded = encodeUuids(json); userConfiguration.updateOne( Filters.eq("_id", d.get("_id")), Filters.eq("$set", Filters.eq("body", jsonEncoded))
BasicDBObject update = (BasicDBObject)doc; update.remove(updateKey); collection.updateOne(query, update, new UpdateOptions().upsert(upsert));
updateOptions.upsert(true); Consumer<MongoClient> upsert = client -> client.getDatabase(DB_NAME).getCollection(this.getCollectionName()) .updateOne(Document.parse("{'_id' : 2}"), setOnInsert, updateOptions); .updateOne(Document.parse("{'_id' : 2}"), setOnInsertAndSet, updateOptions); primary().execute("update", upsertAndUpdate);
Document filter = Document.parse("{\"a\": 1}"); Document operation = Document.parse("{ \"$set\": { \"b\": 10 } }"); coll.updateOne(filter, operation);