加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

java – 在spring数据mongodb中的2d球体索引

发布时间:2020-12-15 01:29:56 所属栏目:大数据 来源:网络整理
导读:我正在研究mongodb的地理空间查询,我已经在mongo控制台上执行了以下查询 db.items.find( { location: { $near : { $geometry: { type: "Point",coordinates: [77.026638,28.459497 ] },$maxDistance: 10000 } } }) 但是如何使用spring数据mongodb标准编写此

我正在研究mongodb的地理空间查询,我已经在mongo控制台上执行了以下查询

db.items.find(
   {
     location:
       { $near :
          {
            $geometry: { type: "Point",coordinates: [77.026638,28.459497 ] },$maxDistance: 10000
          }
       }
   }
)

但是如何使用spring数据mongodb标准编写此查询?

我尝试使用以下标准,但它无法正常工作

 Criteria criteria = Criteria.where("location").near(new Point(77.026638,28.459497)).maxDistance(1000);

        Query query = new Query(criteria);
System.out.println(mongoTemplate.find(query,Item.class));

堆栈跟踪

org.springframework.data.mongodb.UncategorizedMongoDbException: Unable to execute query: error processing query: ns=geolocation.items limit=0 skip=0
Tree: GEONEAR  field=location maxdist=1.79769e+308 isNearSphere=0
Sort: {}
Proj: {}
 planner returned error: unable to find index for $geoNear query; nested exception is com.mongodb.MongoException: Unable to execute query: error processing query: ns=geolocation.items limit=0 skip=0
Tree: GEONEAR  field=location maxdist=1.79769e+308 isNearSphere=0
Sort: {}
Proj: {}
 planner returned error: unable to find index for $geoNear query
    at org.springframework.data.mongodb.core.MongoExceptionTranslator.translateExceptionIfPossible(MongoExceptionTranslator.java:90)
    at org.springframework.data.mongodb.core.MongoTemplate.potentiallyConvertRuntimeException(MongoTemplate.java:1940)
    at org.springframework.data.mongodb.core.MongoTemplate.executeFindMultiInternal(MongoTemplate.java:1823)
    at org.springframework.data.mongodb.core.MongoTemplate.doFind(MongoTemplate.java:1633)
    at org.springframework.data.mongodb.core.MongoTemplate.doFind(MongoTemplate.java:1616)
    at org.springframework.data.mongodb.core.MongoTemplate.find(MongoTemplate.java:535)
    at org.springframework.data.mongodb.core.MongoTemplate.find(MongoTemplate.java:526)
    at com.samepinch.dao.item.ItemDao$$EPWN0nhQ.getItemsByLocation(ItemDao.java:200)
    at com.samepinch.dao.item.ItemDao$$DPWN0nhQ.getItemsByLocation(Unknown Source)
    at com.samepinch.dao.item.ItemDao.getItemsByLocation(ItemDao.java)
    at com.samepinch.services.item.ItemService.getItemsByLocation(ItemService.java:410)
    at com.samepinch.controllers.item.ItemController.getItemsbyLocation(ItemController.java:222)
    at com.samepinch.controllers.item.ItemController$$FastClassBySpringCGLIB$$33bec54c.invoke(

我的架构是

@Document(collection = "items")
public class Item extends BaseEntity{
    private static final long serialVersionUID = 1L;

    private String itemName;
    private MetadataEnum categoryName;
    private List

嵌入式文件是:)

public class Location {

    private List

即使我找到系统索引确保

{ "v" : 1,"key" : { "_id" : 1 },"name" : "_id_","ns" : "geolocation.notification" }
{ "v" : 1,"ns" : "geolocation.preference" }
{ "v" : 1,"ns" : "geolocation.userItemHistory" }
{ "v" : 1,"ns" : "geolocation.media" }
{ "v" : 1,"ns" : "geolocation.userBasedRecommendation" }
{ "v" : 1,"ns" : "geolocation.users" }
{ "v" : 1,"ns" : "geolocation.authenticationToken" }
{ "v" : 1,"unique" : true,"key" : { "username" : 1 },"name" : "username","key" : { "email" : 1 },"name" : "email","ns" : "geolocation.userToUserHistory" }
{ "v" : 1,"ns" : "geolocation.metadata" }
{ "v" : 1,"ns" : "geolocation.mongo_data_model_map" }
{ "v" : 1,"key" : { "element_id" : 1 },"name" : "element_id_1","key" : { "long_value" : 1 },"name" : "long_value_1","ns" : "geolocation.preferenceOrder" }
{ "v" : 1,"ns" : "geolocation.counter" }
{ "v" : 1,"ns" : "geolocation.item" }
{ "v" : 1,"key" : { "location" : "2dsphere" },"name" : "location_2dsphere","ns" : "geolocation.item","2dsphereIndexVersion" : 2 }
{ "v" : 1,"ns" : "geolocation.items" }
{ "v" : 1,"ns" : "geolocation.items","2dsphereIndexVersion" : 2 }
Type "it" for more
> it
{ "v" : 1,"key" : { "a" : 1 },"name" : "a_1","ns" : "geolocation.items" }

我的数据库记录看起来像

{
    "_id" : NumberLong(46602),"_class" : "com.samepinch.domain.item.Item","itemName" : "Chandigarh","categoryName" : "TRAVELLING","attributes" : [
        "MOUNTAIN"
    ],"isAccessed" : false,"imageUrl" : "0bbd8cdb-be5c-4efc-b974-c04fb1903537itemcompressed.jpg","catagoryPreference" : "BOTH","startDate" : ISODate("2015-12-09T18:30:00Z"),"endDate" : ISODate("2015-12-09T18:30:00Z"),"location" : {
        "coordinates" : [
            76.76641440000003,30.72642125251779
        ],"type" : "Point","radius" : 8504.948210235696
    },"createdDate" : ISODate("2015-12-09T10:27:42.519Z"),"updatedDate" : ISODate("2015-12-09T10:27:42.519Z")
}

我已经在集合上创建了2d spheare索引,但我不知道它为什么会给我这个错误.

最佳答案
更改数据存储而不是:

"location" : {
    "coordinates" : [
        76.76641440000003,30.72642125251779
    ],"radius" : 8504.948210235696
}

"location" : {
    "coordinates" : [
        76.76641440000003,"type" : "Point"
}

我认为对于“Point”类型你有错误的数据类型.它应该只有地理坐标的经度,纬度.你不应该包括“raduis”.

检查有效GeoJsons.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读