Skip to content

Commit 01cf9fb

Browse files
Thomas Darimontodrotbohm
Thomas Darimont
authored andcommitted
DATAMONGO-938 - Apply QueryMapper in MongoTemplate.mapReduce(…).
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: spring-projects#184.
1 parent 285c406 commit 01cf9fb

File tree

2 files changed

+30
-3
lines changed

2 files changed

+30
-3
lines changed

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1455,13 +1455,13 @@ private DBObject copyQuery(Query query, DBObject copyMapReduceOptions) {
14551455
"Can not use skip or field specification with map reduce operations");
14561456
}
14571457
if (query.getQueryObject() != null) {
1458-
copyMapReduceOptions.put("query", query.getQueryObject());
1458+
copyMapReduceOptions.put("query", queryMapper.getMappedObject(query.getQueryObject(), null));
14591459
}
14601460
if (query.getLimit() > 0) {
14611461
copyMapReduceOptions.put("limit", query.getLimit());
14621462
}
14631463
if (query.getSortObject() != null) {
1464-
copyMapReduceOptions.put("sort", query.getSortObject());
1464+
copyMapReduceOptions.put("sort", queryMapper.getMappedObject(query.getSortObject(), null));
14651465
}
14661466
}
14671467
return copyMapReduceOptions;

spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/mapreduce/MapReduceTests.java

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2011 the original author or authors.
2+
* Copyright 2011-2014 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.
@@ -32,6 +32,7 @@
3232
import org.junit.Test;
3333
import org.junit.runner.RunWith;
3434
import org.springframework.beans.factory.annotation.Autowired;
35+
import org.springframework.data.geo.Box;
3536
import org.springframework.data.mongodb.MongoDbFactory;
3637
import org.springframework.data.mongodb.core.MongoTemplate;
3738
import org.springframework.data.mongodb.core.convert.DbRefResolver;
@@ -50,6 +51,7 @@
5051
* Integration test for {@link MongoTemplate}'s Map-Reduce operations
5152
*
5253
* @author Mark Pollack
54+
* @author Thomas Darimont
5355
*/
5456
@RunWith(SpringJUnit4ClassRunner.class)
5557
@ContextConfiguration("classpath:infrastructure.xml")
@@ -276,6 +278,31 @@ public void testMapReduceExcludeQuery() {
276278

277279
}
278280

281+
/**
282+
* @see DATAMONGO-938
283+
*/
284+
@Test
285+
public void mapReduceShouldUseQueryMapper() {
286+
287+
DBCollection c = mongoTemplate.getDb().getCollection("jmrWithGeo");
288+
289+
c.save(new BasicDBObject("x", new String[] { "a", "b" }).append("loc", new double[] { 0, 0 }));
290+
c.save(new BasicDBObject("x", new String[] { "b", "c" }).append("loc", new double[] { 0, 0 }));
291+
c.save(new BasicDBObject("x", new String[] { "c", "d" }).append("loc", new double[] { 0, 0 }));
292+
293+
Query query = new Query(where("x").ne(new String[] { "a", "b" }).and("loc")
294+
.within(new Box(new double[] { 0, 0 }, new double[] { 1, 1 })));
295+
296+
MapReduceResults<ValueObject> results = template.mapReduce(query, "jmrWithGeo", mapFunction, reduceFunction,
297+
ValueObject.class);
298+
299+
Map<String, Float> m = copyToMap(results);
300+
assertEquals(3, m.size());
301+
assertEquals(1, m.get("b").intValue());
302+
assertEquals(2, m.get("c").intValue());
303+
assertEquals(1, m.get("d").intValue());
304+
}
305+
279306
private void performMapReduce(boolean inline, boolean withQuery) {
280307
createMapReduceData();
281308
MapReduceResults<ValueObject> results;

0 commit comments

Comments
 (0)