Skip to content

Commit b4753f3

Browse files
committed
DATAMONGO-1360 - Query instances contained in a Near Query now get mapped during geoNear(…) execution.
A Query instance which might be part of a NearQuery definition is now passed through the QueryMapper to make sure complex types contained in it or even in more general types that have custom conversions registered are mapped correctly before the near command is actually executed.
1 parent bce6e2c commit b4753f3

File tree

3 files changed

+31
-4
lines changed

3 files changed

+31
-4
lines changed

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

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -654,8 +654,15 @@ public <T> GeoResults<T> geoNear(NearQuery near, Class<T> entityClass, String co
654654
}
655655

656656
String collection = StringUtils.hasText(collectionName) ? collectionName : determineCollectionName(entityClass);
657+
DBObject nearDbObject = near.toDBObject();
658+
657659
BasicDBObject command = new BasicDBObject("geoNear", collection);
658-
command.putAll(near.toDBObject());
660+
command.putAll(nearDbObject);
661+
662+
if (nearDbObject.containsField("query")) {
663+
DBObject query = (DBObject) nearDbObject.get("query");
664+
command.put("query", queryMapper.getMappedObject(query, getPersistentEntity(entityClass)));
665+
}
659666

660667
CommandResult commandResult = executeCommand(command, this.readPreference);
661668
List<Object> results = (List<Object>) commandResult.get("results");

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

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,17 +17,18 @@
1717

1818
import java.util.Arrays;
1919

20+
import org.joda.time.LocalDate;
2021
import org.springframework.data.annotation.Id;
2122
import org.springframework.data.annotation.PersistenceConstructor;
2223
import org.springframework.data.mongodb.core.mapping.Document;
2324

2425
@Document(collection = "newyork")
2526
public class Venue {
2627

27-
@Id
28-
private String id;
28+
@Id private String id;
2929
private String name;
3030
private double[] location;
31+
private LocalDate openingDate;
3132

3233
@PersistenceConstructor
3334
Venue(String name, double[] location) {
@@ -50,6 +51,14 @@ public double[] getLocation() {
5051
return location;
5152
}
5253

54+
public LocalDate getOpeningDate() {
55+
return openingDate;
56+
}
57+
58+
public void setOpeningDate(LocalDate openingDate) {
59+
this.openingDate = openingDate;
60+
}
61+
5362
@Override
5463
public String toString() {
5564
return "Venue [id=" + id + ", name=" + name + ", location=" + Arrays.toString(location) + "]";

spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/geo/AbstractGeoSpatialTests.java

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2015 the original author or authors.
2+
* Copyright 2015-2016 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.
@@ -22,6 +22,7 @@
2222

2323
import java.util.List;
2424

25+
import org.joda.time.LocalDate;
2526
import org.junit.After;
2627
import org.junit.Before;
2728
import org.junit.Test;
@@ -49,6 +50,7 @@
4950

5051
/**
5152
* @author Christoph Strobl
53+
* @author Oliver Gierke
5254
*/
5355
@RunWith(SpringJUnit4ClassRunner.class)
5456
@ContextConfiguration
@@ -173,4 +175,13 @@ public void nearSphere() {
173175
assertThat(venues.size(), is(11));
174176
}
175177

178+
/**
179+
* @see DATAMONGO-1360
180+
*/
181+
@Test
182+
public void mapsQueryContainedInNearQuery() {
183+
184+
Query query = query(where("openingDate").lt(LocalDate.now()));
185+
template.geoNear(NearQuery.near(1.5, 1.7).query(query), Venue.class);
186+
}
176187
}

0 commit comments

Comments
 (0)