Skip to content

Commit

Permalink
Fixed #1202 (#1203)
Browse files Browse the repository at this point in the history
  • Loading branch information
alexwirz authored and evanchooly committed Sep 24, 2018
1 parent 49f646f commit 389cf53
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,16 @@ public interface UpdateOperations<T> {
*/
UpdateOperations<T> addToSet(String field, List<?> values);

/**
* adds the values to an array field if they doesn't already exist in the array
*
* @param field the field to update
* @param values the values to add
* @return this
* @mongodb.driver.manual reference/operator/update/addToSet/ $addToSet
*/
UpdateOperations<T> addToSet(String field, Iterable<?> values);

/**
* Decrements the numeric field by 1
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import java.util.Map;

import static java.util.Collections.singletonList;
import static org.mongodb.morphia.utils.ReflectionUtils.iterToList;


/**
Expand Down Expand Up @@ -91,6 +92,11 @@ public UpdateOperations<T> addToSet(final String field, final List<?> values) {
return this;
}

@Override
public UpdateOperations<T> addToSet(final String field, final Iterable<?> values) {
return addToSet(field, iterToList(values));
}

@Override
public UpdateOperations<T> push(final String field, final Object value) {
return push(field, value instanceof List ? (List<?>) value : singletonList(value), new PushOptions());
Expand Down
6 changes: 6 additions & 0 deletions morphia/src/test/java/org/mongodb/morphia/TestUpdateOps.java
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,12 @@ public void testAddToSet() throws Exception {
.addToSet("values", asList(4, 5))),
1);
assertThat(getDs().get(cIntArray).values, is(new Integer[]{1, 2, 3, 5, 4, 8, 9}));

assertUpdated(getDs().update(getDs().find(ContainsIntArray.class),
getDs().createUpdateOperations(ContainsIntArray.class)
.addToSet("values", new HashSet<Integer>(asList(10, 11)))),
1);
assertThat(getDs().get(cIntArray).values, is(new Integer[]{1, 2, 3, 5, 4, 8, 9, 10, 11}));
}

@Test
Expand Down

0 comments on commit 389cf53

Please sign in to comment.