From 8b1805a1458c019f5ffa786df5cde7f956a9b053 Mon Sep 17 00:00:00 2001 From: Oliver Gierke Date: Thu, 10 Dec 2015 15:38:40 +0100 Subject: [PATCH] =?UTF-8?q?DATAMONGO-1346=20-=20Update.pullAll(=E2=80=A6)?= =?UTF-8?q?=20now=20registers=20multiple=20invocations=20correctly.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Previously calling the method multiple times overrode the result of previous calls. We now use addMultiFieldOperation(…) to make sure already existing values are kept. --- .../data/mongodb/core/query/Update.java | 12 +++++++++- .../data/mongodb/core/query/UpdateTests.java | 22 ++++++++++++++++++- 2 files changed, 32 insertions(+), 2 deletions(-) diff --git a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/query/Update.java b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/query/Update.java index 6911d953a..00e35280b 100644 --- a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/query/Update.java +++ b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/query/Update.java @@ -254,7 +254,7 @@ public class Update { * @return */ 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; } @@ -330,9 +330,19 @@ public class Update { 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) { Assert.hasText(key, "Key/Path for update must not be null or blank."); + modifierOps.put(operator, new BasicDBObject(key, value)); this.keysToUpdate.add(key); } diff --git a/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/query/UpdateTests.java b/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/query/UpdateTests.java index 9f7429dfa..37d937032 100644 --- a/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/query/UpdateTests.java +++ b/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"); * 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.junit.Test; +import org.springframework.data.mongodb.core.DBObjectTestUtils; import com.mongodb.BasicDBObject; import com.mongodb.BasicDBObjectBuilder; +import com.mongodb.DBObject; /** * Test cases for {@link Update}. @@ -484,4 +486,22 @@ public class UpdateTests { public void pushShouldThrowExceptionWhenGivenNegativePosition() { 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())); + } }