Skip to content

Commit 9feac13

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

File tree

2 files changed

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

2 files changed

+125
-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
@@ -314,6 +314,34 @@ public Update multiply(String key, Number multiplier) {
314314
addMultiFieldOperation("$mul", key, multiplier.doubleValue());
315315
return this;
316316
}
317+
318+
/**
319+
* Update using the {@literal $max} update modifier
320+
*
321+
* @see http://docs.mongodb.org/manual/reference/operator/update/max/
322+
* @param key
323+
* @param value
324+
* @return
325+
*/
326+
public Update max(String key, Object value) {
327+
Assert.notNull(value, "Value must not be 'null'.");
328+
addMultiFieldOperation("$max", key, value);
329+
return this;
330+
}
331+
332+
/**
333+
* Update using the {@literal $max} update modifier
334+
*
335+
* @see http://docs.mongodb.org/manual/reference/operator/update/min/
336+
* @param key
337+
* @param value
338+
* @return
339+
*/
340+
public Update min(String key, Object value) {
341+
Assert.notNull(value, "Value must not be 'null'.");
342+
addMultiFieldOperation("$min", key, value);
343+
return this;
344+
}
317345

318346
/**
319347
* 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: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -504,4 +504,101 @@ public void registersMultiplePullAllClauses() {
504504
assertThat(pullAll.get("field1"), is(notNullValue()));
505505
assertThat(pullAll.get("field2"), is(notNullValue()));
506506
}
507+
508+
509+
/**
510+
* @see DATAMONGO-1404
511+
*/
512+
@Test(expected = IllegalArgumentException.class)
513+
public void maxShouldThrowExceptionForNullMultiplier() {
514+
new Update().max("key", null);
515+
}
516+
517+
/**
518+
* @see DATAMONGO-1404
519+
*/
520+
@Test(expected = IllegalArgumentException.class)
521+
public void minShouldThrowExceptionForNullMultiplier() {
522+
new Update().min("key", null);
523+
}
524+
525+
/**
526+
* @see DATAMONGO-1404
527+
*/
528+
@Test
529+
public void getUpdateObjectShouldReturnCorrectRepresentationForMax() {
530+
531+
Update update = new Update().max("key", 10);
532+
533+
assertThat(update.getUpdateObject(), equalTo(new BasicDBObjectBuilder().add("$max", new BasicDBObject("key", 10))
534+
.get()));
535+
}
536+
537+
/**
538+
* @see DATAMONGO-1404
539+
*/
540+
@Test
541+
public void getUpdateObjectShouldReturnCorrectRepresentationForMin() {
542+
543+
Update update = new Update().min("key", 10);
544+
545+
assertThat(update.getUpdateObject(), equalTo(new BasicDBObjectBuilder().add("$min", new BasicDBObject("key", 10))
546+
.get()));
547+
}
548+
549+
/**
550+
* @see DATAMONGO-1404
551+
*/
552+
@Test
553+
public void shouldSuppressPreviousValueForMax() {
554+
555+
Update update = new Update().max("key", 10);
556+
557+
update.max("key", 99);
558+
559+
assertThat(update.getUpdateObject(), equalTo(new BasicDBObjectBuilder().add("$max", new BasicDBObject("key", 99))
560+
.get()));
561+
}
562+
563+
/**
564+
* @see DATAMONGO-1404
565+
*/
566+
@Test
567+
public void shouldSuppressPreviousValueForMin() {
568+
569+
Update update = new Update().min("key", 10);
570+
571+
update.max("key", 99);
572+
573+
assertThat(update.getUpdateObject(), equalTo(new BasicDBObjectBuilder().add("$min9", new BasicDBObject("key", 99))
574+
.get()));
575+
}
576+
577+
/**
578+
* @see DATAMONGO-1404
579+
*/
580+
@Test
581+
public void getUpdateObjectShouldReturnCorrectDateRepresentationForMax() {
582+
583+
final java.util.Date date = new java.util.Date();
584+
585+
Update update = new Update().max("key", date);
586+
587+
assertThat(update.getUpdateObject(), equalTo(new BasicDBObjectBuilder().add("$max", new BasicDBObject("key", date))
588+
.get()));
589+
}
590+
591+
/**
592+
* @see DATAMONGO-1404
593+
*/
594+
@Test
595+
public void getUpdateObjectShouldReturnCorrectDateRepresentationForMin() {
596+
597+
final java.util.Date date = new java.util.Date();
598+
599+
Update update = new Update().min("key", date);
600+
601+
assertThat(update.getUpdateObject(), equalTo(new BasicDBObjectBuilder().add("$min", new BasicDBObject("key", date))
602+
.get()));
603+
}
507604
}

0 commit comments

Comments
 (0)