Browse Source

DATAMONGO-1346 - Update.pullAll(…) now registers multiple invocations correctly.

Previously calling the method multiple times overrode the result of previous calls. We now use addMultiFieldOperation(…) to make sure already existing values are kept.
1.7.x
Oliver Gierke 10 years ago
parent
commit
3e5b4f25a4
  1. 12
      spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/query/Update.java
  2. 22
      spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/query/UpdateTests.java

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

@ -254,7 +254,7 @@ public class Update {
* @return * @return
*/ */
public Update pullAll(String key, Object[] values) { public Update pullAll(String key, Object[] values) {
addFieldOperation("$pullAll", key, Arrays.copyOf(values, values.length)); addMultiFieldOperation("$pullAll", key, Arrays.copyOf(values, values.length));
return this; return this;
} }
@ -330,9 +330,19 @@ public class Update {
return new BasicDBObject(modifierOps); return new BasicDBObject(modifierOps);
} }
/**
* This method is not called anymore rather override {@link #addMultiFieldOperation(String, String, Object)}.
*
* @param operator
* @param key
* @param value
* @deprectaed Use {@link #addMultiFieldOperation(String, String, Object)} instead.
*/
@Deprecated
protected void addFieldOperation(String operator, String key, Object value) { protected void addFieldOperation(String operator, String key, Object value) {
Assert.hasText(key, "Key/Path for update must not be null or blank."); Assert.hasText(key, "Key/Path for update must not be null or blank.");
modifierOps.put(operator, new BasicDBObject(key, value)); modifierOps.put(operator, new BasicDBObject(key, value));
this.keysToUpdate.add(key); this.keysToUpdate.add(key);
} }

22
spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/query/UpdateTests.java

@ -1,5 +1,5 @@
/* /*
* Copyright 2010-2014 the original author or authors. * Copyright 2010-2015 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -23,9 +23,11 @@ import java.util.Map;
import org.joda.time.DateTime; import org.joda.time.DateTime;
import org.junit.Test; import org.junit.Test;
import org.springframework.data.mongodb.core.DBObjectTestUtils;
import com.mongodb.BasicDBObject; import com.mongodb.BasicDBObject;
import com.mongodb.BasicDBObjectBuilder; import com.mongodb.BasicDBObjectBuilder;
import com.mongodb.DBObject;
/** /**
* Test cases for {@link Update}. * Test cases for {@link Update}.
@ -484,4 +486,22 @@ public class UpdateTests {
public void pushShouldThrowExceptionWhenGivenNegativePosition() { public void pushShouldThrowExceptionWhenGivenNegativePosition() {
new Update().push("foo").atPosition(-1).each("booh"); new Update().push("foo").atPosition(-1).each("booh");
} }
/**
* @see DATAMONGO-1346
*/
@Test
public void registersMultiplePullAllClauses() {
Update update = new Update();
update.pullAll("field1", new String[] { "foo" });
update.pullAll("field2", new String[] { "bar" });
DBObject updateObject = update.getUpdateObject();
DBObject pullAll = DBObjectTestUtils.getAsDBObject(updateObject, "$pullAll");
assertThat(pullAll.get("field1"), is(notNullValue()));
assertThat(pullAll.get("field2"), is(notNullValue()));
}
} }

Loading…
Cancel
Save