Skip to content

DATAMONGO-471 - Add support for '$each' when using '$addToSet'. #157

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

<groupId>org.springframework.data</groupId>
<artifactId>spring-data-mongodb-parent</artifactId>
<version>1.5.0.BUILD-SNAPSHOT</version>
<version>1.5.0.DATAMONGO-471-SNAPSHOT</version>
<packaging>pom</packaging>

<name>Spring Data MongoDB</name>
Expand Down
4 changes: 2 additions & 2 deletions spring-data-mongodb-cross-store/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<parent>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-mongodb-parent</artifactId>
<version>1.5.0.BUILD-SNAPSHOT</version>
<version>1.5.0.DATAMONGO-471-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>

Expand Down Expand Up @@ -48,7 +48,7 @@
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-mongodb</artifactId>
<version>1.5.0.BUILD-SNAPSHOT</version>
<version>1.5.0.DATAMONGO-471-SNAPSHOT</version>
</dependency>

<dependency>
Expand Down
2 changes: 1 addition & 1 deletion spring-data-mongodb-distribution/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
<parent>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-mongodb-parent</artifactId>
<version>1.5.0.BUILD-SNAPSHOT</version>
<version>1.5.0.DATAMONGO-471-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>

Expand Down
2 changes: 1 addition & 1 deletion spring-data-mongodb-log4j/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<parent>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-mongodb-parent</artifactId>
<version>1.5.0.BUILD-SNAPSHOT</version>
<version>1.5.0.DATAMONGO-471-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>

Expand Down
2 changes: 1 addition & 1 deletion spring-data-mongodb/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
<parent>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-mongodb-parent</artifactId>
<version>1.5.0.BUILD-SNAPSHOT</version>
<version>1.5.0.DATAMONGO-471-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,18 @@ public Update pushAll(String key, Object[] values) {
return this;
}

/**
* Update using {@code $addToSet} modifier. <br/>
* Allows creation of {@code $push} command for single or multiple (using {@code $each}) values
*
* @param key
* @return
* @since 1.5
*/
public AddToSetBuilder addToSet(String key) {
return new AddToSetBuilder(key);
}

/**
* Update using the {@literal $addToSet} update modifier
*
Expand Down Expand Up @@ -408,7 +420,7 @@ public Object getValue() {
/**
* Builder for creating {@code $push} modifiers
*
* @author Christop Strobl
* @author Christoph Strobl
*/
public class PushOperatorBuilder {

Expand Down Expand Up @@ -442,4 +454,41 @@ public Update value(Object value) {
return Update.this.push(key, value);
}
}

/**
* Builder for creating {@code $addToSet} modifier.
*
* @author Christoph Strobl
* @since 1.5
*/
public class AddToSetBuilder {

private final String key;

public AddToSetBuilder(String key) {
this.key = key;
}

/**
* Propagates {@code $each} to {@code $addToSet}
*
* @param values
* @return
*/
public Update each(Object... values) {
return Update.this.addToSet(this.key, new Each(values));
}

/**
* Propagates {@link #value(Object)} to {@code $addToSet}
*
* @param values
* @return
*/
public Update value(Object value) {
return Update.this.addToSet(this.key, value);
}

}

}
Original file line number Diff line number Diff line change
Expand Up @@ -2550,6 +2550,25 @@ public void savingAndReassigningLazyLoadingProxies() {
assertThat(savedMessage.normalContent.text, is(content.text));
}

/**
* @see DATAMONGO-471
*/
@Test
public void updateMultiShouldAddValuesCorrectlyWhenUsingAddToSetWithEach() {

DocumentWithCollectionOfSimpleType document = new DocumentWithCollectionOfSimpleType();
document.values = Arrays.asList("spring");
template.save(document);

Query query = query(where("id").is(document.id));
assumeThat(template.findOne(query, DocumentWithCollectionOfSimpleType.class).values, hasSize(1));

Update update = new Update().addToSet("values").each("data", "mongodb");
template.updateMulti(query, update, DocumentWithCollectionOfSimpleType.class);

assertThat(template.findOne(query, DocumentWithCollectionOfSimpleType.class).values, hasSize(3));
}

static class DocumentWithDBRefCollection {

@Id public String id;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -412,6 +412,46 @@ public void doesNotConvertRawDbObjects() {
assertThat(inClause, IsIterableContainingInOrder.<Object> contains(1L, 2L));
}

/**
* @see DATAMONG0-471
*/
@SuppressWarnings({ "unchecked", "rawtypes" })
@Test
public void testUpdateShouldApply$addToSetCorrectlyWhenUsedWith$each() {

Update update = new Update().addToSet("values").each("spring", "data", "mongodb");
DBObject mappedObject = mapper.getMappedObject(update.getUpdateObject(),
context.getPersistentEntity(ListModel.class));

DBObject addToSet = getAsDBObject(mappedObject, "$addToSet");
DBObject values = getAsDBObject(addToSet, "values");
BasicDBList each = getAsDBList(values, "$each");

assertThat(each.toMap(), (Matcher) allOf(hasValue("spring"), hasValue("data"), hasValue("mongodb")));
}

/**
* @see DATAMONG0-471
*/
@Test
public void testUpdateShouldRetainClassTypeInformationWhenUsing$addToSetWith$eachForCustomTypes() {

Update update = new Update().addToSet("models").each(new ModelImpl(2014), new ModelImpl(1), new ModelImpl(28));
DBObject mappedObject = mapper.getMappedObject(update.getUpdateObject(),
context.getPersistentEntity(ModelWrapper.class));

DBObject addToSet = getAsDBObject(mappedObject, "$addToSet");

DBObject values = getAsDBObject(addToSet, "models");
BasicDBList each = getAsDBList(values, "$each");

for (Object updateValue : each) {
assertThat(((DBObject) updateValue).get("_class").toString(),
equalTo("org.springframework.data.mongodb.core.convert.UpdateMapperUnitTests$ModelImpl"));
}

}

static interface Model {}

static class ModelImpl implements Model {
Expand All @@ -425,6 +465,12 @@ public ModelImpl(int value) {

public class ModelWrapper {
Model model;

public ModelWrapper() {}

public ModelWrapper(Model model) {
this.model = model;
}
}

static class ListModelWrapper {
Expand Down