Skip to content

Commit 8b1805a

Browse files
committed
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 parent 5832055 commit 8b1805a

File tree

2 files changed

+32
-2
lines changed
  • spring-data-mongodb/src

2 files changed

+32
-2
lines changed

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

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -254,7 +254,7 @@ public Update pull(String key, Object value) {
254254
* @return
255255
*/
256256
public Update pullAll(String key, Object[] values) {
257-
addFieldOperation("$pullAll", key, Arrays.copyOf(values, values.length));
257+
addMultiFieldOperation("$pullAll", key, Arrays.copyOf(values, values.length));
258258
return this;
259259
}
260260

@@ -330,9 +330,19 @@ public DBObject getUpdateObject() {
330330
return new BasicDBObject(modifierOps);
331331
}
332332

333+
/**
334+
* This method is not called anymore rather override {@link #addMultiFieldOperation(String, String, Object)}.
335+
*
336+
* @param operator
337+
* @param key
338+
* @param value
339+
* @deprectaed Use {@link #addMultiFieldOperation(String, String, Object)} instead.
340+
*/
341+
@Deprecated
333342
protected void addFieldOperation(String operator, String key, Object value) {
334343

335344
Assert.hasText(key, "Key/Path for update must not be null or blank.");
345+
336346
modifierOps.put(operator, new BasicDBObject(key, value));
337347
this.keysToUpdate.add(key);
338348
}

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

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2010-2014 the original author or authors.
2+
* Copyright 2010-2015 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -23,9 +23,11 @@
2323

2424
import org.joda.time.DateTime;
2525
import org.junit.Test;
26+
import org.springframework.data.mongodb.core.DBObjectTestUtils;
2627

2728
import com.mongodb.BasicDBObject;
2829
import com.mongodb.BasicDBObjectBuilder;
30+
import com.mongodb.DBObject;
2931

3032
/**
3133
* Test cases for {@link Update}.
@@ -484,4 +486,22 @@ public void getUpdateObjectShouldReturnCorrectRepresentationForBitwiseXor() {
484486
public void pushShouldThrowExceptionWhenGivenNegativePosition() {
485487
new Update().push("foo").atPosition(-1).each("booh");
486488
}
489+
490+
/**
491+
* @see DATAMONGO-1346
492+
*/
493+
@Test
494+
public void registersMultiplePullAllClauses() {
495+
496+
Update update = new Update();
497+
update.pullAll("field1", new String[] { "foo" });
498+
update.pullAll("field2", new String[] { "bar" });
499+
500+
DBObject updateObject = update.getUpdateObject();
501+
502+
DBObject pullAll = DBObjectTestUtils.getAsDBObject(updateObject, "$pullAll");
503+
504+
assertThat(pullAll.get("field1"), is(notNullValue()));
505+
assertThat(pullAll.get("field2"), is(notNullValue()));
506+
}
487507
}

0 commit comments

Comments
 (0)