use of com.mongodb.client.result.UpdateResult in project spring-data-mongodb by spring-projects.
the class ExecutableUpdateOperationSupportTests method updateAllMatching.
// DATAMONGO-1563
@Test
public void updateAllMatching() {
UpdateResult result = template.update(Person.class).matching(queryHan()).apply(new Update().set("firstname", "Han")).all();
assertThat(result.getModifiedCount()).isEqualTo(1L);
assertThat(result.getUpsertedId()).isNull();
}
use of com.mongodb.client.result.UpdateResult in project spring-data-mongodb by spring-projects.
the class MongoTemplateTests method testUsingUpdateWithMultipleSet.
@Test
public void testUsingUpdateWithMultipleSet() throws Exception {
template.remove(new Query(), PersonWithIdPropertyOfTypeObjectId.class);
PersonWithIdPropertyOfTypeObjectId p1 = new PersonWithIdPropertyOfTypeObjectId();
p1.setFirstName("Sven");
p1.setAge(11);
template.insert(p1);
PersonWithIdPropertyOfTypeObjectId p2 = new PersonWithIdPropertyOfTypeObjectId();
p2.setFirstName("Mary");
p2.setAge(21);
template.insert(p2);
Update u = new Update().set("firstName", "Bob").set("age", 10);
UpdateResult wr = template.updateMulti(new Query(), u, PersonWithIdPropertyOfTypeObjectId.class);
if (wr.wasAcknowledged()) {
assertThat(wr.getModifiedCount(), is(2L));
}
Query q1 = new Query(Criteria.where("age").in(11, 21));
List<PersonWithIdPropertyOfTypeObjectId> r1 = template.find(q1, PersonWithIdPropertyOfTypeObjectId.class);
assertThat(r1.size(), is(0));
Query q2 = new Query(Criteria.where("age").is(10));
List<PersonWithIdPropertyOfTypeObjectId> r2 = template.find(q2, PersonWithIdPropertyOfTypeObjectId.class);
assertThat(r2.size(), is(2));
for (PersonWithIdPropertyOfTypeObjectId p : r2) {
assertThat(p.getAge(), is(10));
assertThat(p.getFirstName(), is("Bob"));
}
}
use of com.mongodb.client.result.UpdateResult in project spring-data-mongodb by spring-projects.
the class MongoTemplate method doSaveVersioned.
private <T> T doSaveVersioned(T objectToSave, MongoPersistentEntity<?> entity, String collectionName) {
ConvertingPropertyAccessor convertingAccessor = new ConvertingPropertyAccessor(entity.getPropertyAccessor(objectToSave), mongoConverter.getConversionService());
MongoPersistentProperty property = entity.getRequiredVersionProperty();
Number number = convertingAccessor.getProperty(property, Number.class);
if (number != null) {
// Bump version number
convertingAccessor.setProperty(property, number.longValue() + 1);
maybeEmitEvent(new BeforeConvertEvent<T>(objectToSave, collectionName));
assertUpdateableIdIfNotSet(objectToSave);
Document document = new Document();
this.mongoConverter.write(objectToSave, document);
maybeEmitEvent(new BeforeSaveEvent<T>(objectToSave, document, collectionName));
Update update = Update.fromDocument(document, ID_FIELD);
// Create query for entity with the id and old version
MongoPersistentProperty idProperty = entity.getRequiredIdProperty();
Object id = entity.getIdentifierAccessor(objectToSave).getRequiredIdentifier();
Query query = new Query(Criteria.where(idProperty.getName()).is(id).and(property.getName()).is(number));
UpdateResult result = doUpdate(collectionName, query, update, objectToSave.getClass(), false, false);
if (result.getModifiedCount() == 0) {
throw new OptimisticLockingFailureException(String.format("Cannot save entity %s with version %s to collection %s. Has it been modified meanwhile?", id, number, collectionName));
}
maybeEmitEvent(new AfterSaveEvent<T>(objectToSave, document, collectionName));
return objectToSave;
}
doInsert(collectionName, objectToSave, this.mongoConverter);
return objectToSave;
}
use of com.mongodb.client.result.UpdateResult in project graylog2-server by Graylog2.
the class V20170110150100_FixAlertConditionsMigration method upgrade.
@Override
@SuppressWarnings("unchecked")
public void upgrade() {
if (clusterConfigService.get(MigrationCompleted.class) != null) {
LOG.debug("Migration already done.");
return;
}
final ImmutableSet.Builder<String> modifiedStreams = ImmutableSet.builder();
final ImmutableSet.Builder<String> modifiedAlertConditions = ImmutableSet.builder();
for (Document document : collection.find().sort(ascending(FIELD_CREATED_AT))) {
final String streamId = document.getObjectId(FIELD_ID).toHexString();
if (!document.containsKey(FIELD_ALERT_CONDITIONS)) {
continue;
}
final List<Document> alertConditions = (List<Document>) document.get(FIELD_ALERT_CONDITIONS);
// Need to check if the following fields are integers:
//
// FieldContentValue: grace, backlog
// FieldValue: grace, backlog, time, threshold
// MessageCount: grace, backlog, time, threshold
final Set<String> intFields = ImmutableSet.of("grace", "backlog", "time", "threshold");
for (Document alertCondition : alertConditions) {
final String alertConditionId = alertCondition.get("id", String.class);
final String alertConditionTitle = alertCondition.get("title", String.class);
final Document parameters = alertCondition.get("parameters", Document.class);
for (String field : intFields) {
final Object fieldValue = parameters.get(field);
// No need to convert anything if the field does not exist or is already an integer
if (fieldValue == null || fieldValue instanceof Integer) {
continue;
}
if (!(fieldValue instanceof String)) {
LOG.warn("Field <{}> in alert condition <{}> ({}) of stream <{}> is not a string but a <{}>, not trying to convert it!", field, alertConditionId, alertConditionTitle, streamId, fieldValue.getClass().getCanonicalName());
continue;
}
final String stringValue = parameters.get(field, String.class);
final Integer intValue = Ints.tryParse(stringValue);
LOG.info("Converting value for field <{}> from string to integer in alert condition <{}> ({}) of stream <{}>", field, alertConditionId, alertConditionTitle, streamId);
if (intValue == null) {
LOG.error("Unable to parse \"{}\" into integer!", fieldValue);
}
final UpdateResult result = collection.updateOne(eq(FIELD_ALERT_CONDITIONS_ID, alertConditionId), set(ALERT_CONDITIONS_PARAMETERS_PREFIX + field, intValue));
// Use UpdateResult#getMatchedCount() instead of #getModifiedCount() to make it work on MongoDB 2.4
if (result.getMatchedCount() > 0) {
modifiedStreams.add(streamId);
modifiedAlertConditions.add(alertConditionId);
} else {
LOG.warn("No document modified for alert condition <{}> ({})", alertConditionId, alertConditionTitle);
}
}
}
}
clusterConfigService.write(MigrationCompleted.create(modifiedStreams.build(), modifiedAlertConditions.build()));
}
use of com.mongodb.client.result.UpdateResult in project mongo-java-driver by mongodb.
the class CrudTest method toResult.
private BsonDocument toResult(final MongoOperationUpdateResult operation) {
UpdateResult updateResult = operation.get();
BsonDocument resultDoc = new BsonDocument("matchedCount", new BsonInt32((int) updateResult.getMatchedCount()));
if (updateResult.isModifiedCountAvailable()) {
resultDoc.append("modifiedCount", new BsonInt32((int) updateResult.getModifiedCount()));
}
// in replaceOne-pre_2.6
if (updateResult.getUpsertedId() != null && !updateResult.getUpsertedId().isObjectId()) {
resultDoc.append("upsertedId", updateResult.getUpsertedId());
}
resultDoc.append("upsertedCount", updateResult.getUpsertedId() == null ? new BsonInt32(0) : new BsonInt32(1));
return toResult(resultDoc);
}
Aggregations