Browse Source

DATAMONGO-1097 - Add support for $mul to Update.

We now support multiply on Update allowing to multiply the value of the given key by a multiplier.
pull/249/merge
Christoph Strobl 11 years ago committed by Thomas Darimont
parent
commit
457fda3fc3
  1. 16
      spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/query/Update.java
  2. 20
      spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/query/UpdateTests.java

16
spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/query/Update.java

@ -308,6 +308,22 @@ public class Update { @@ -308,6 +308,22 @@ public class Update {
return this;
}
/**
* Multiply the value of given key by the given number.
*
* @see http://docs.mongodb.org/manual/reference/operator/update/mul/
* @param key must not be {@literal null}.
* @param multiplier must not be {@literal null}.
* @return
* @since 1.7
*/
public Update multiply(String key, Number multiplier) {
Assert.notNull(multiplier, "Multiplier must not be 'null'.");
addMultiFieldOperation("$mul", key, multiplier.doubleValue());
return this;
}
public DBObject getUpdateObject() {
DBObject dbo = new BasicDBObject();

20
spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/query/UpdateTests.java

@ -420,4 +420,24 @@ public class UpdateTests { @@ -420,4 +420,24 @@ public class UpdateTests {
Update update = new Update().addToSet("key", new DateTime());
assertThat(update.toString(), is(notNullValue()));
}
/**
* @see DATAMONGO-1097
*/
@Test(expected = IllegalArgumentException.class)
public void multiplyShouldThrowExceptionForNullMultiplier() {
new Update().multiply("key", null);
}
/**
* @see DATAMONGO-1097
*/
@Test
public void multiplyShouldAddMultiplierAsItsDoubleValue() {
Update update = new Update().multiply("key", 10);
assertThat(update.getUpdateObject(), equalTo(new BasicDBObjectBuilder().add("$mul", new BasicDBObject("key", 10D))
.get()));
}
}

Loading…
Cancel
Save