Example usage for org.springframework.data.mongodb.core.query NearQuery getMetric

List of usage examples for org.springframework.data.mongodb.core.query NearQuery getMetric

Introduction

In this page you can find the example usage for org.springframework.data.mongodb.core.query NearQuery getMetric.

Prototype

public Metric getMetric() 

Source Link

Document

Returns the Metric underlying the actual query.

Usage

From source file:org.springframework.data.mongodb.core.MongoTemplate.java

@SuppressWarnings("unchecked")
public <T> GeoResults<T> geoNear(NearQuery near, Class<T> entityClass, String collectionName) {

    if (near == null) {
        throw new InvalidDataAccessApiUsageException("NearQuery must not be null!");
    }/* w  w  w .  ja  v a 2s  . c o m*/

    if (entityClass == null) {
        throw new InvalidDataAccessApiUsageException("Entity class must not be null!");
    }

    String collection = StringUtils.hasText(collectionName) ? collectionName
            : determineCollectionName(entityClass);
    BasicDBObject command = new BasicDBObject("geoNear", collection);
    command.putAll(near.toDBObject());

    CommandResult commandResult = executeCommand(command);
    List<Object> results = (List<Object>) commandResult.get("results");
    results = results == null ? Collections.emptyList() : results;

    DbObjectCallback<GeoResult<T>> callback = new GeoNearResultDbObjectCallback<T>(
            new ReadDbObjectCallback<T>(mongoConverter, entityClass), near.getMetric());
    List<GeoResult<T>> result = new ArrayList<GeoResult<T>>(results.size());

    for (Object element : results) {
        result.add(callback.doWith((DBObject) element));
    }

    DBObject stats = (DBObject) commandResult.get("stats");
    double averageDistance = stats == null ? 0 : (Double) stats.get("avgDistance");
    return new GeoResults<T>(result, new Distance(averageDistance, near.getMetric()));
}