Skip to content

Commit 116baf9

Browse files
DATAMONGO-832 - Polishing
Moved newly introduced types into order. Added missing @SInCE tag and additional test. Updated reference documentation for update operators and added $slice operator to "what’s new" section. Original Pull Request: #374
1 parent 026dce2 commit 116baf9

File tree

4 files changed

+78
-32
lines changed

4 files changed

+78
-32
lines changed

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

Lines changed: 31 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -603,6 +603,31 @@ public boolean equals(Object that) {
603603
}
604604
}
605605

606+
/**
607+
* {@link Modifier} implementation used to propagate {@code $position}.
608+
*
609+
* @author Christoph Strobl
610+
* @since 1.7
611+
*/
612+
private static class PositionModifier implements Modifier {
613+
614+
private final int position;
615+
616+
public PositionModifier(int position) {
617+
this.position = position;
618+
}
619+
620+
@Override
621+
public String getKey() {
622+
return "$position";
623+
}
624+
625+
@Override
626+
public Object getValue() {
627+
return position;
628+
}
629+
}
630+
606631
/**
607632
* Implementation of {@link Modifier} representing {@code $slice}.
608633
*
@@ -636,31 +661,6 @@ public Object getValue() {
636661
}
637662
}
638663

639-
/**
640-
* {@link Modifier} implementation used to propagate {@code $position}.
641-
*
642-
* @author Christoph Strobl
643-
* @since 1.7
644-
*/
645-
private static class PositionModifier implements Modifier {
646-
647-
private final int position;
648-
649-
public PositionModifier(int position) {
650-
this.position = position;
651-
}
652-
653-
@Override
654-
public String getKey() {
655-
return "$position";
656-
}
657-
658-
@Override
659-
public Object getValue() {
660-
return position;
661-
}
662-
}
663-
664664
/**
665665
* Builder for creating {@code $push} modifiers
666666
*
@@ -681,7 +681,7 @@ public class PushOperatorBuilder {
681681
* Propagates {@code $each} to {@code $push}
682682
*
683683
* @param values
684-
* @return
684+
* @return never {@literal null}.
685685
*/
686686
public Update each(Object... values) {
687687

@@ -698,7 +698,8 @@ public Update each(Object... values) {
698698
* elements. <br />
699699
*
700700
* @param count
701-
* @return
701+
* @return never {@literal null}.
702+
* @since 1.10
702703
*/
703704
public PushOperatorBuilder slice(int count) {
704705

@@ -710,7 +711,7 @@ public PushOperatorBuilder slice(int count) {
710711
* Forces values to be added at the given {@literal position}.
711712
*
712713
* @param position needs to be greater than or equal to zero.
713-
* @return
714+
* @return never {@literal null}.
714715
* @since 1.7
715716
*/
716717
public PushOperatorBuilder atPosition(int position) {
@@ -728,7 +729,7 @@ public PushOperatorBuilder atPosition(int position) {
728729
* Forces values to be added at given {@literal position}.
729730
*
730731
* @param position can be {@literal null} which will be appended at the last position.
731-
* @return
732+
* @return never {@literal null}.
732733
* @since 1.7
733734
*/
734735
public PushOperatorBuilder atPosition(Position position) {
@@ -746,7 +747,7 @@ public PushOperatorBuilder atPosition(Position position) {
746747
* Propagates {@link #value(Object)} to {@code $push}
747748
*
748749
* @param values
749-
* @return
750+
* @return never {@literal null}.
750751
*/
751752
public Update value(Object value) {
752753
return Update.this.push(key, value);

spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/convert/UpdateMapperUnitTests.java

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -388,7 +388,32 @@ public void updatePushEachWithSliceShouldRenderCorrectly() {
388388

389389
assertThat(key.containsField("$slice"), is(true));
390390
assertThat((Integer) key.get("$slice"), is(5));
391-
assertThat(getAsDBObject(push, "key").containsField("$each"), is(true));
391+
assertThat(key.containsField("$each"), is(true));
392+
}
393+
394+
/**
395+
* @see DATAMONGO-832
396+
*/
397+
@Test
398+
public void updatePushEachWithSliceShouldRenderWhenUsingMultiplePushCorrectly() {
399+
400+
Update update = new Update().push("key").slice(5).each(Arrays.asList("Arya", "Arry", "Weasel")).push("key-2")
401+
.slice(-2).each("The Beggar King", "Viserys III Targaryen");
402+
403+
DBObject mappedObject = mapper.getMappedObject(update.getUpdateObject(), context.getPersistentEntity(Object.class));
404+
405+
DBObject push = getAsDBObject(mappedObject, "$push");
406+
DBObject key = getAsDBObject(push, "key");
407+
408+
assertThat(key.containsField("$slice"), is(true));
409+
assertThat((Integer) key.get("$slice"), is(5));
410+
assertThat(key.containsField("$each"), is(true));
411+
412+
DBObject key2 = getAsDBObject(push, "key-2");
413+
414+
assertThat(key2.containsField("$slice"), is(true));
415+
assertThat((Integer) key2.get("$slice"), is(-2));
416+
assertThat(key2.containsField("$each"), is(true));
392417
}
393418

394419
/**

src/main/asciidoc/new-features.adoc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
[[new-features.1-10-0]]
55
== What's new in Spring Data MongoDB 1.10
6-
* Support for `$min` and `$max` operators to `Update`.
6+
* Support for `$min`, `$max` and `$slice` operators via `Update`.
77

88
[[new-features.1-9-0]]
99
== What's new in Spring Data MongoDB 1.9

src/main/asciidoc/reference/mongodb.adoc

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -879,6 +879,26 @@ Here is a listing of methods on the Update class
879879
* `Update` *setOnInsert* `(String key, Object value)` Update using the `$setOnInsert` update modifier
880880
* `Update` *unset* `(String key)` Update using the `$unset` update modifier
881881

882+
Some update modifiers like `$push` and `$addToSet` allow nesting of additional operators.
883+
884+
[source]
885+
----
886+
// { $push : { "category" : { "$each" : [ "spring" , "data" ] } } }
887+
new Update().push("category").each("spring", "data")
888+
889+
// { $push : { "key" : { "$position" : 0 , "$each" : [ "Arya" , "Arry" , "Weasel" ] } } }
890+
new Update().push("key").atPosition(Position.FIRST).each(Arrays.asList("Arya", "Arry", "Weasel"));
891+
892+
// { $push : { "key" : { "$slice" : 5 , "$each" : [ "Arya" , "Arry" , "Weasel" ] } } }
893+
new Update().push("key").slice(5).each(Arrays.asList("Arya", "Arry", "Weasel"));
894+
----
895+
896+
[source]
897+
----
898+
// { $addToSet : { "values" : { "$each" : [ "spring" , "data" , "mongodb" ] } } }
899+
new Update().addToSet("values").each("spring", "data", "mongodb");
900+
----
901+
882902
[[mongo-template.upserts]]
883903
=== Upserting documents in a collection
884904

0 commit comments

Comments
 (0)