Browse Source

DATAMONGO-1404 - Add support for $min and $max update operators.

Original pull request: #353.
CLA: 169820160330091912 (Alexey Plotnik)
pull/359/merge
Alexey Plotnik 10 years ago committed by Christoph Strobl
parent
commit
8983bd26ce
  1. 28
      spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/query/Update.java
  2. 97
      spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/query/UpdateTests.java

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

@ -314,6 +314,34 @@ public class Update { @@ -314,6 +314,34 @@ public class Update {
addMultiFieldOperation("$mul", key, multiplier.doubleValue());
return this;
}
/**
* Update using the {@literal $max} update modifier
*
* @see http://docs.mongodb.org/manual/reference/operator/update/max/
* @param key
* @param value
* @return
*/
public Update max(String key, Object value) {
Assert.notNull(value, "Value must not be 'null'.");
addMultiFieldOperation("$max", key, value);
return this;
}
/**
* Update using the {@literal $max} update modifier
*
* @see http://docs.mongodb.org/manual/reference/operator/update/min/
* @param key
* @param value
* @return
*/
public Update min(String key, Object value) {
Assert.notNull(value, "Value must not be 'null'.");
addMultiFieldOperation("$min", key, value);
return this;
}
/**
* The operator supports bitwise {@code and}, bitwise {@code or}, and bitwise {@code xor} operations.

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

@ -504,4 +504,101 @@ public class UpdateTests { @@ -504,4 +504,101 @@ public class UpdateTests {
assertThat(pullAll.get("field1"), is(notNullValue()));
assertThat(pullAll.get("field2"), is(notNullValue()));
}
/**
* @see DATAMONGO-1404
*/
@Test(expected = IllegalArgumentException.class)
public void maxShouldThrowExceptionForNullMultiplier() {
new Update().max("key", null);
}
/**
* @see DATAMONGO-1404
*/
@Test(expected = IllegalArgumentException.class)
public void minShouldThrowExceptionForNullMultiplier() {
new Update().min("key", null);
}
/**
* @see DATAMONGO-1404
*/
@Test
public void getUpdateObjectShouldReturnCorrectRepresentationForMax() {
Update update = new Update().max("key", 10);
assertThat(update.getUpdateObject(), equalTo(new BasicDBObjectBuilder().add("$max", new BasicDBObject("key", 10))
.get()));
}
/**
* @see DATAMONGO-1404
*/
@Test
public void getUpdateObjectShouldReturnCorrectRepresentationForMin() {
Update update = new Update().min("key", 10);
assertThat(update.getUpdateObject(), equalTo(new BasicDBObjectBuilder().add("$min", new BasicDBObject("key", 10))
.get()));
}
/**
* @see DATAMONGO-1404
*/
@Test
public void shouldSuppressPreviousValueForMax() {
Update update = new Update().max("key", 10);
update.max("key", 99);
assertThat(update.getUpdateObject(), equalTo(new BasicDBObjectBuilder().add("$max", new BasicDBObject("key", 99))
.get()));
}
/**
* @see DATAMONGO-1404
*/
@Test
public void shouldSuppressPreviousValueForMin() {
Update update = new Update().min("key", 10);
update.max("key", 99);
assertThat(update.getUpdateObject(), equalTo(new BasicDBObjectBuilder().add("$min9", new BasicDBObject("key", 99))
.get()));
}
/**
* @see DATAMONGO-1404
*/
@Test
public void getUpdateObjectShouldReturnCorrectDateRepresentationForMax() {
final java.util.Date date = new java.util.Date();
Update update = new Update().max("key", date);
assertThat(update.getUpdateObject(), equalTo(new BasicDBObjectBuilder().add("$max", new BasicDBObject("key", date))
.get()));
}
/**
* @see DATAMONGO-1404
*/
@Test
public void getUpdateObjectShouldReturnCorrectDateRepresentationForMin() {
final java.util.Date date = new java.util.Date();
Update update = new Update().min("key", date);
assertThat(update.getUpdateObject(), equalTo(new BasicDBObjectBuilder().add("$min", new BasicDBObject("key", date))
.get()));
}
}

Loading…
Cancel
Save