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.
This commit is contained in:
Christoph Strobl
2014-11-18 20:53:27 +01:00
committed by Thomas Darimont
parent 54cee64610
commit 457fda3fc3
2 changed files with 36 additions and 0 deletions

View File

@@ -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();

View File

@@ -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()));
}
}