Skip to content

Commit e3256e1

Browse files
odiszapcchristophstrobl
authored andcommitted
DATAMONGO-1404 - Add support for $min and $max update operators.
Original pull request: #353. CLA: 169820160330091912 (Alexey Plotnik)
1 parent 76d04c3 commit e3256e1

File tree

2 files changed

+118
-0
lines changed
  • spring-data-mongodb/src

2 files changed

+118
-0
lines changed

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

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -312,6 +312,34 @@ public Update multiply(String key, Number multiplier) {
312312
addMultiFieldOperation("$mul", key, multiplier.doubleValue());
313313
return this;
314314
}
315+
316+
/**
317+
* Update using the {@literal $max} update modifier
318+
*
319+
* @see http://docs.mongodb.org/manual/reference/operator/update/max/
320+
* @param key
321+
* @param value
322+
* @return
323+
*/
324+
public Update max(String key, Object value) {
325+
Assert.notNull(value, "Value must not be 'null'.");
326+
addMultiFieldOperation("$max", key, value);
327+
return this;
328+
}
329+
330+
/**
331+
* Update using the {@literal $max} update modifier
332+
*
333+
* @see http://docs.mongodb.org/manual/reference/operator/update/min/
334+
* @param key
335+
* @param value
336+
* @return
337+
*/
338+
public Update min(String key, Object value) {
339+
Assert.notNull(value, "Value must not be 'null'.");
340+
addMultiFieldOperation("$min", key, value);
341+
return this;
342+
}
315343

316344
/**
317345
* The operator supports bitwise {@code and}, bitwise {@code or}, and bitwise {@code xor} operations.

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

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -490,4 +490,94 @@ public void registersMultiplePullAllClauses() {
490490
assertThat(pullAll.get("field1"), is(notNullValue()));
491491
assertThat(pullAll.get("field2"), is(notNullValue()));
492492
}
493+
494+
/**
495+
* @see DATAMONGO-1404
496+
*/
497+
@Test(expected = IllegalArgumentException.class)
498+
public void maxShouldThrowExceptionForNullMultiplier() {
499+
new Update().max("key", null);
500+
}
501+
502+
/**
503+
* @see DATAMONGO-1404
504+
*/
505+
@Test(expected = IllegalArgumentException.class)
506+
public void minShouldThrowExceptionForNullMultiplier() {
507+
new Update().min("key", null);
508+
}
509+
510+
/**
511+
* @see DATAMONGO-1404
512+
*/
513+
@Test
514+
public void getUpdateObjectShouldReturnCorrectRepresentationForMax() {
515+
516+
Update update = new Update().max("key", 10);
517+
518+
assertThat(update.getUpdateObject(), equalTo(new Document().append("$max", new Document("key", 10))));
519+
}
520+
521+
/**
522+
* @see DATAMONGO-1404
523+
*/
524+
@Test
525+
public void getUpdateObjectShouldReturnCorrectRepresentationForMin() {
526+
527+
Update update = new Update().min("key", 10);
528+
529+
assertThat(update.getUpdateObject(), equalTo(new Document().append("$min", new Document("key", 10))));
530+
}
531+
532+
/**
533+
* @see DATAMONGO-1404
534+
*/
535+
@Test
536+
public void shouldSuppressPreviousValueForMax() {
537+
538+
Update update = new Update().max("key", 10);
539+
540+
update.max("key", 99);
541+
542+
assertThat(update.getUpdateObject(), equalTo(new Document().append("$max", new Document("key", 99))));
543+
}
544+
545+
/**
546+
* @see DATAMONGO-1404
547+
*/
548+
@Test
549+
public void shouldSuppressPreviousValueForMin() {
550+
551+
Update update = new Update().min("key", 10);
552+
553+
update.max("key", 99);
554+
555+
assertThat(update.getUpdateObject(), equalTo(new Document().append("$min9", new Document("key", 99))));
556+
}
557+
558+
/**
559+
* @see DATAMONGO-1404
560+
*/
561+
@Test
562+
public void getUpdateObjectShouldReturnCorrectDateRepresentationForMax() {
563+
564+
final java.util.Date date = new java.util.Date();
565+
566+
Update update = new Update().max("key", date);
567+
568+
assertThat(update.getUpdateObject(), equalTo(new Document().append("$max", new Document("key", date))));
569+
}
570+
571+
/**
572+
* @see DATAMONGO-1404
573+
*/
574+
@Test
575+
public void getUpdateObjectShouldReturnCorrectDateRepresentationForMin() {
576+
577+
final java.util.Date date = new java.util.Date();
578+
579+
Update update = new Update().min("key", date);
580+
581+
assertThat(update.getUpdateObject(), equalTo(new Document().append("$min", new Document("key", date))));
582+
}
493583
}

0 commit comments

Comments
 (0)