Browse Source

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
pull/375/merge
Christoph Strobl 10 years ago
parent
commit
116baf9a92
  1. 61
      spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/query/Update.java
  2. 27
      spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/convert/UpdateMapperUnitTests.java
  3. 2
      src/main/asciidoc/new-features.adoc
  4. 20
      src/main/asciidoc/reference/mongodb.adoc

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

@ -603,6 +603,31 @@ public class Update { @@ -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 { @@ -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 { @@ -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 { @@ -698,7 +698,8 @@ public class Update {
* elements. <br />
*
* @param count
* @return
* @return never {@literal null}.
* @since 1.10
*/
public PushOperatorBuilder slice(int count) {
@ -710,7 +711,7 @@ public class Update { @@ -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 { @@ -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 { @@ -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);

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

@ -388,7 +388,32 @@ public class UpdateMapperUnitTests { @@ -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));
}
/**

2
src/main/asciidoc/new-features.adoc

@ -3,7 +3,7 @@ @@ -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

20
src/main/asciidoc/reference/mongodb.adoc

@ -879,6 +879,26 @@ Here is a listing of methods on the Update class @@ -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

Loading…
Cancel
Save