MongoDB集合中的2dsphere索引不是在Spring Java中获取的

时间:2016-06-02 06:31:52

标签: java spring mongodb

我和[这里] [1]有同样的问题。我创建了一个索引“2dsphere”,但是从spring Java中获取它时,它告诉我在该特定列上创建一个索引来触发find()地理空间查询。你能告诉我如何从spring Java获取地理空间索引吗?

这是我在mongodb中的收藏结构:

{
"_id" : ObjectId("5729f6f0daf5583ade68b277"),
"type" : "Feature",
"geometry" : {
    "type" : "Polygon",
    "coordinates" : [ 
        [ 
            [ 
                -61.392147064209, 
                12.5836229324341
            ], 
            [ 
                -61.3972930908203, 
                12.5838899612427
            ], 
            [ 
                -61.401252746582, 
                12.5869073867798
            ], 
            [ 
                -61.3977165222168, 
                12.5903301239014
            ], 
            [ 
                -61.3904685974121, 
                12.5893573760986
            ], 
            [ 
                -61.388843536377, 
                12.587965965271
            ], 
            [ 
                -61.3890991210938, 
                12.5845184326172
            ], 
            [ 
                -61.392147064209, 
                12.5836229324341
            ]
        ]
    ]
},
"properties" : {
    "TZID" : "America/St_Vincent"
}
}

以及创建索引查询的查询:

db.timezone.createIndex({"geometry":"2dsphere"})

@Repository
public class MongoDaoImpl extends BaseMongoDaoImpl implements MongoDao{

public static final Logger logger=LoggerFactory.getLogger(MongoDaoImpl.class);

@Autowired
MongoTemplate mongoTemplate;

@Override
public String getTimezoneIdFromLatLong(Double lat, Double longi){
    String TZId=null;
    try{
        Query query = new Query(new Criteria()
                        .and("geometry")
                        .near(new Point(lat, longi)).maxDistance(0)).limit(1);
        query.fields().include("properties.TZID").exclude("_id");   
        logger.info("Mongo query : " + query.toString());
        TZId =  mongoTemplate.find(query, String.class,"timezone").get(0);

        logger.info("Getting the TimezoneId of Lat and long : "+ TZId);
    }catch(Exception e){
        logger.error("Exception occured while getting TimezoneId from lat & long", e);
    }
    return TZId;

}
}

所以,我试图运行上面的代码然后它在控制台中给我以下错误。

  

2016-06-03 12:15:52,185 [http-nio-8321-exec-3:a7827b0e-eb2e-4485-a385-e6a8a1dd765d] INFO :::, - Mongo查询:查询:{“geometry”: {“$ near”:{$ java:Point [x = -61.338249,y = 12.691054]},“$ maxDistance”:0.0}},字段:{“properties.TZID”:1,“_ id”:0},排序:null

     

524e89638c4d] ERROR :::, - 从lat&获取TimezoneId时发生异常长   org.springframework.data.mongodb.UncategorizedMongoDbException:查询失败,错误代码为2,错误消息'错误处理查询:ns = services.timezone limit = 1Tree:GEONEAR字段= geometry maxdist = 0 isNearSphere = 0   排序:{}   Proj:{properties.TZID:1,_id:0}    planner返回错误:无法在服务器localhost:27017上找到$ geoNear query的索引;嵌套异常是com.mongodb.MongoQueryException:查询失败,错误代码为2,错误消息'错误处理查询:ns = services.timezone limit = 1Tree:GEONEAR字段= geometry maxdist = 0 isNearSphere = 0   排序:{}   Proj:{properties.TZID:1,_id:0}    planner返回错误:无法在服务器localhost:27017上找到$ geoNear query'的索引       在org.springframework.data.mongodb.core.MongoExceptionTranslator.translateExceptionIfPossible(MongoExceptionTranslator.java:107)〜[spring-data-mongodb-1.9.1.RELEASE.jar:na]       在org.springframework.data.mongodb.core.MongoTemplate.potentiallyConvertRuntimeException(MongoTemplate.java:2114)〜[spring-data-mongodb-1.9.1.RELEASE.jar:na]

1 个答案:

答案 0 :(得分:0)

经过这么多的努力,我找到了答案而没有使用Criteria。以下是不使用弹簧...即。只有java这绝对会帮助你

MongoClient mongoClient = new MongoClient("localhost" , 27017 );
DB db = mongoClient.getDB("services");
DBCollection collection=  db.getCollection("timezone");
BasicDBObject point = new BasicDBObject("type", "Point");
double[] ptCordinate={lat,longi};
point.put("coordinates", ptCordinate);

BasicDBObject query = new BasicDBObject(
    "geometry", new BasicDBObject(
        "$near", new BasicDBObject(
            "$geometry", point
        )
    )
);
collection.findOne(query);