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:
committed by
Thomas Darimont
parent
54cee64610
commit
457fda3fc3
@@ -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();
|
||||
|
||||
@@ -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()));
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user