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.
This commit is contained in:
Oliver Gierke
2016-01-20 13:10:50 +01:00
parent bce6e2c78c
commit b4753f3a83
3 changed files with 31 additions and 4 deletions

View File

@@ -654,8 +654,15 @@ public class MongoTemplate implements MongoOperations, ApplicationContextAware {
}
String collection = StringUtils.hasText(collectionName) ? collectionName : determineCollectionName(entityClass);
DBObject nearDbObject = near.toDBObject();
BasicDBObject command = new BasicDBObject("geoNear", collection);
command.putAll(near.toDBObject());
command.putAll(nearDbObject);
if (nearDbObject.containsField("query")) {
DBObject query = (DBObject) nearDbObject.get("query");
command.put("query", queryMapper.getMappedObject(query, getPersistentEntity(entityClass)));
}
CommandResult commandResult = executeCommand(command, this.readPreference);
List<Object> results = (List<Object>) commandResult.get("results");

View File

@@ -17,6 +17,7 @@ package org.springframework.data.mongodb.core;
import java.util.Arrays;
import org.joda.time.LocalDate;
import org.springframework.data.annotation.Id;
import org.springframework.data.annotation.PersistenceConstructor;
import org.springframework.data.mongodb.core.mapping.Document;
@@ -24,10 +25,10 @@ import org.springframework.data.mongodb.core.mapping.Document;
@Document(collection = "newyork")
public class Venue {
@Id
private String id;
@Id private String id;
private String name;
private double[] location;
private LocalDate openingDate;
@PersistenceConstructor
Venue(String name, double[] location) {
@@ -50,6 +51,14 @@ public class Venue {
return location;
}
public LocalDate getOpeningDate() {
return openingDate;
}
public void setOpeningDate(LocalDate openingDate) {
this.openingDate = openingDate;
}
@Override
public String toString() {
return "Venue [id=" + id + ", name=" + name + ", location=" + Arrays.toString(location) + "]";

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2015 the original author or authors.
* Copyright 2015-2016 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.
@@ -22,6 +22,7 @@ import static org.springframework.data.mongodb.core.query.Query.*;
import java.util.List;
import org.joda.time.LocalDate;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
@@ -49,6 +50,7 @@ import com.mongodb.WriteConcern;
/**
* @author Christoph Strobl
* @author Oliver Gierke
*/
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration
@@ -173,4 +175,13 @@ public abstract class AbstractGeoSpatialTests {
assertThat(venues.size(), is(11));
}
/**
* @see DATAMONGO-1360
*/
@Test
public void mapsQueryContainedInNearQuery() {
Query query = query(where("openingDate").lt(LocalDate.now()));
template.geoNear(NearQuery.near(1.5, 1.7).query(query), Venue.class);
}
}