From 01cf9fb8f3af47afdb9de109bf86f1b64e5096b3 Mon Sep 17 00:00:00 2001 From: Thomas Darimont Date: Thu, 22 May 2014 15:46:33 +0200 Subject: [PATCH] =?UTF-8?q?DATAMONGO-938=20-=20Apply=20QueryMapper=20in=20?= =?UTF-8?q?MongoTemplate.mapReduce(=E2=80=A6).?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Previously MongoTemplate.mapReduce(...) didn't translate nested objects, e.g. GeoCommand, within the given query. That could lead to exceptions during query serialization. We now pass the query and sort object of the given Query through the QueryMapper to avoid such problems. Original pull request: #184. --- .../data/mongodb/core/MongoTemplate.java | 4 +-- .../core/mapreduce/MapReduceTests.java | 29 ++++++++++++++++++- 2 files changed, 30 insertions(+), 3 deletions(-) diff --git a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/MongoTemplate.java b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/MongoTemplate.java index 4037f4650..3bc8e75d3 100644 --- a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/MongoTemplate.java +++ b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/MongoTemplate.java @@ -1455,13 +1455,13 @@ public class MongoTemplate implements MongoOperations, ApplicationContextAware { "Can not use skip or field specification with map reduce operations"); } if (query.getQueryObject() != null) { - copyMapReduceOptions.put("query", query.getQueryObject()); + copyMapReduceOptions.put("query", queryMapper.getMappedObject(query.getQueryObject(), null)); } if (query.getLimit() > 0) { copyMapReduceOptions.put("limit", query.getLimit()); } if (query.getSortObject() != null) { - copyMapReduceOptions.put("sort", query.getSortObject()); + copyMapReduceOptions.put("sort", queryMapper.getMappedObject(query.getSortObject(), null)); } } return copyMapReduceOptions; diff --git a/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/mapreduce/MapReduceTests.java b/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/mapreduce/MapReduceTests.java index 88b80d7a7..5ebd4861d 100644 --- a/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/mapreduce/MapReduceTests.java +++ b/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/mapreduce/MapReduceTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2011 the original author or authors. + * Copyright 2011-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. @@ -32,6 +32,7 @@ import org.junit.Ignore; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.data.geo.Box; import org.springframework.data.mongodb.MongoDbFactory; import org.springframework.data.mongodb.core.MongoTemplate; import org.springframework.data.mongodb.core.convert.DbRefResolver; @@ -50,6 +51,7 @@ import com.mongodb.Mongo; * Integration test for {@link MongoTemplate}'s Map-Reduce operations * * @author Mark Pollack + * @author Thomas Darimont */ @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration("classpath:infrastructure.xml") @@ -276,6 +278,31 @@ public class MapReduceTests { } + /** + * @see DATAMONGO-938 + */ + @Test + public void mapReduceShouldUseQueryMapper() { + + DBCollection c = mongoTemplate.getDb().getCollection("jmrWithGeo"); + + c.save(new BasicDBObject("x", new String[] { "a", "b" }).append("loc", new double[] { 0, 0 })); + c.save(new BasicDBObject("x", new String[] { "b", "c" }).append("loc", new double[] { 0, 0 })); + c.save(new BasicDBObject("x", new String[] { "c", "d" }).append("loc", new double[] { 0, 0 })); + + Query query = new Query(where("x").ne(new String[] { "a", "b" }).and("loc") + .within(new Box(new double[] { 0, 0 }, new double[] { 1, 1 }))); + + MapReduceResults results = template.mapReduce(query, "jmrWithGeo", mapFunction, reduceFunction, + ValueObject.class); + + Map m = copyToMap(results); + assertEquals(3, m.size()); + assertEquals(1, m.get("b").intValue()); + assertEquals(2, m.get("c").intValue()); + assertEquals(1, m.get("d").intValue()); + } + private void performMapReduce(boolean inline, boolean withQuery) { createMapReduceData(); MapReduceResults results;