From 116baf9a92f78b0894d97ba1923fda2fc12a469e Mon Sep 17 00:00:00 2001 From: Christoph Strobl Date: Mon, 11 Jul 2016 09:46:18 +0200 Subject: [PATCH] DATAMONGO-832 - Polishing MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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 --- .../data/mongodb/core/query/Update.java | 61 ++++++++++--------- .../core/convert/UpdateMapperUnitTests.java | 27 +++++++- src/main/asciidoc/new-features.adoc | 2 +- src/main/asciidoc/reference/mongodb.adoc | 20 ++++++ 4 files changed, 78 insertions(+), 32 deletions(-) diff --git a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/query/Update.java b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/query/Update.java index 39177729f..44f2718c4 100644 --- a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/query/Update.java +++ b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/query/Update.java @@ -603,6 +603,31 @@ public class Update { } } + /** + * {@link Modifier} implementation used to propagate {@code $position}. + * + * @author Christoph Strobl + * @since 1.7 + */ + private static class PositionModifier implements Modifier { + + private final int position; + + public PositionModifier(int position) { + this.position = position; + } + + @Override + public String getKey() { + return "$position"; + } + + @Override + public Object getValue() { + return position; + } + } + /** * Implementation of {@link Modifier} representing {@code $slice}. * @@ -636,31 +661,6 @@ public class Update { } } - /** - * {@link Modifier} implementation used to propagate {@code $position}. - * - * @author Christoph Strobl - * @since 1.7 - */ - private static class PositionModifier implements Modifier { - - private final int position; - - public PositionModifier(int position) { - this.position = position; - } - - @Override - public String getKey() { - return "$position"; - } - - @Override - public Object getValue() { - return position; - } - } - /** * Builder for creating {@code $push} modifiers * @@ -681,7 +681,7 @@ public class Update { * Propagates {@code $each} to {@code $push} * * @param values - * @return + * @return never {@literal null}. */ public Update each(Object... values) { @@ -698,7 +698,8 @@ public class Update { * elements.
* * @param count - * @return + * @return never {@literal null}. + * @since 1.10 */ public PushOperatorBuilder slice(int count) { @@ -710,7 +711,7 @@ public class Update { * Forces values to be added at the given {@literal position}. * * @param position needs to be greater than or equal to zero. - * @return + * @return never {@literal null}. * @since 1.7 */ public PushOperatorBuilder atPosition(int position) { @@ -728,7 +729,7 @@ public class Update { * Forces values to be added at given {@literal position}. * * @param position can be {@literal null} which will be appended at the last position. - * @return + * @return never {@literal null}. * @since 1.7 */ public PushOperatorBuilder atPosition(Position position) { @@ -746,7 +747,7 @@ public class Update { * Propagates {@link #value(Object)} to {@code $push} * * @param values - * @return + * @return never {@literal null}. */ public Update value(Object value) { return Update.this.push(key, value); diff --git a/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/convert/UpdateMapperUnitTests.java b/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/convert/UpdateMapperUnitTests.java index 81b8b69aa..1522580b5 100644 --- a/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/convert/UpdateMapperUnitTests.java +++ b/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/convert/UpdateMapperUnitTests.java @@ -388,7 +388,32 @@ public class UpdateMapperUnitTests { assertThat(key.containsField("$slice"), is(true)); assertThat((Integer) key.get("$slice"), is(5)); - assertThat(getAsDBObject(push, "key").containsField("$each"), is(true)); + assertThat(key.containsField("$each"), is(true)); + } + + /** + * @see DATAMONGO-832 + */ + @Test + public void updatePushEachWithSliceShouldRenderWhenUsingMultiplePushCorrectly() { + + Update update = new Update().push("key").slice(5).each(Arrays.asList("Arya", "Arry", "Weasel")).push("key-2") + .slice(-2).each("The Beggar King", "Viserys III Targaryen"); + + DBObject mappedObject = mapper.getMappedObject(update.getUpdateObject(), context.getPersistentEntity(Object.class)); + + DBObject push = getAsDBObject(mappedObject, "$push"); + DBObject key = getAsDBObject(push, "key"); + + assertThat(key.containsField("$slice"), is(true)); + assertThat((Integer) key.get("$slice"), is(5)); + assertThat(key.containsField("$each"), is(true)); + + DBObject key2 = getAsDBObject(push, "key-2"); + + assertThat(key2.containsField("$slice"), is(true)); + assertThat((Integer) key2.get("$slice"), is(-2)); + assertThat(key2.containsField("$each"), is(true)); } /** diff --git a/src/main/asciidoc/new-features.adoc b/src/main/asciidoc/new-features.adoc index fe68c7bed..032b8df74 100644 --- a/src/main/asciidoc/new-features.adoc +++ b/src/main/asciidoc/new-features.adoc @@ -3,7 +3,7 @@ [[new-features.1-10-0]] == What's new in Spring Data MongoDB 1.10 -* Support for `$min` and `$max` operators to `Update`. +* Support for `$min`, `$max` and `$slice` operators via `Update`. [[new-features.1-9-0]] == What's new in Spring Data MongoDB 1.9 diff --git a/src/main/asciidoc/reference/mongodb.adoc b/src/main/asciidoc/reference/mongodb.adoc index 9000e275d..e6697721a 100644 --- a/src/main/asciidoc/reference/mongodb.adoc +++ b/src/main/asciidoc/reference/mongodb.adoc @@ -879,6 +879,26 @@ Here is a listing of methods on the Update class * `Update` *setOnInsert* `(String key, Object value)` Update using the `$setOnInsert` update modifier * `Update` *unset* `(String key)` Update using the `$unset` update modifier +Some update modifiers like `$push` and `$addToSet` allow nesting of additional operators. + +[source] +---- +// { $push : { "category" : { "$each" : [ "spring" , "data" ] } } } +new Update().push("category").each("spring", "data") + +// { $push : { "key" : { "$position" : 0 , "$each" : [ "Arya" , "Arry" , "Weasel" ] } } } +new Update().push("key").atPosition(Position.FIRST).each(Arrays.asList("Arya", "Arry", "Weasel")); + +// { $push : { "key" : { "$slice" : 5 , "$each" : [ "Arya" , "Arry" , "Weasel" ] } } } +new Update().push("key").slice(5).each(Arrays.asList("Arya", "Arry", "Weasel")); +---- + +[source] +---- +// { $addToSet : { "values" : { "$each" : [ "spring" , "data" , "mongodb" ] } } } +new Update().addToSet("values").each("spring", "data", "mongodb"); +---- + [[mongo-template.upserts]] === Upserting documents in a collection