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 0cb4c0ba9..793cbadac 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 @@ -169,13 +169,12 @@ public class Update { * @return */ public Update pushAll(String key, Object[] values) { + Object[] convertedValues = new Object[values.length]; for (int i = 0; i < values.length; i++) { convertedValues[i] = values[i]; } - DBObject keyValue = new BasicDBObject(); - keyValue.put(key, convertedValues); - modifierOps.put("$pushAll", keyValue); + addMultiFieldOperation("$pushAll", key, convertedValues); return this; } diff --git a/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/MongoTemplateTests.java b/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/MongoTemplateTests.java index aa0fa2cf0..11bea41db 100644 --- a/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/MongoTemplateTests.java +++ b/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/MongoTemplateTests.java @@ -184,6 +184,7 @@ public class MongoTemplateTests { template.dropCollection(ObjectWithEnumValue.class); template.dropCollection(DocumentWithCollection.class); template.dropCollection(DocumentWithCollectionOfSimpleType.class); + template.dropCollection(DocumentWithMultipleCollections.class); } @Test @@ -2302,6 +2303,30 @@ public class MongoTemplateTests { assertThat(template.findOne(q, VersionedPerson.class), nullValue()); } + /** + * @see DATAMONGO-354 + */ + @Test + public void testUpdateShouldAllowMultiplePushAll() { + + DocumentWithMultipleCollections doc = new DocumentWithMultipleCollections(); + doc.id = "1234"; + doc.string1 = Arrays.asList("spring"); + doc.string2 = Arrays.asList("one"); + + template.save(doc); + + Update update = new Update().pushAll("string1", new Object[] { "data", "mongodb" }); + update.pushAll("string2", new String[] { "two", "three" }); + + Query findQuery = new Query(Criteria.where("id").is(doc.id)); + template.updateFirst(findQuery, update, DocumentWithMultipleCollections.class); + + DocumentWithMultipleCollections result = template.findOne(findQuery, DocumentWithMultipleCollections.class); + assertThat(result.string1, hasItems("spring", "data", "mongodb")); + assertThat(result.string2, hasItems("one", "two", "three")); + } + static class DocumentWithCollection { @Id String id; @@ -2318,6 +2343,12 @@ public class MongoTemplateTests { List values; } + static class DocumentWithMultipleCollections { + @Id String id; + List string1; + List string2; + } + static interface Model { String value(); } 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 617c750b9..46ec08b73 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-2013 the original author or authors. + * Copyright 2010-2014 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. @@ -29,6 +29,7 @@ import org.junit.Test; * @author Oliver Gierke * @author Thomas Risberg * @author Becca Gaspard + * @author Christoph Strobl */ public class UpdateTests { @@ -94,6 +95,23 @@ public class UpdateTests { is("{ \"$pushAll\" : { \"authors\" : [ { \"name\" : \"Sven\"} , { \"name\" : \"Maria\"}]}}")); } + /** + * @see DATAMONGO-354 + */ + @Test + public void testMultiplePushAllShouldBePossibleWhenUsingDifferentFields() { + + Map m1 = Collections.singletonMap("name", "Sven"); + Map m2 = Collections.singletonMap("name", "Maria"); + + Update u = new Update().pushAll("authors", new Object[] { m1, m2 }); + u.pushAll("books", new Object[] { "Spring in Action" }); + + assertThat( + u.getUpdateObject().toString(), + is("{ \"$pushAll\" : { \"authors\" : [ { \"name\" : \"Sven\"} , { \"name\" : \"Maria\"}] , \"books\" : [ \"Spring in Action\"]}}")); + } + @Test public void testAddToSet() {