Mongodb 需要有2d和2d球体索引,但可以';我们不能创建这两个索引。为什么?
在对MongoDB地理数据查询进行单元测试期间,我收到以下错误消息: 找不到任何特殊索引:2d(需要索引),2d球体(需要索引),for:{address.location:{$nearSphere:{x:-120.0,y:47.0},$maxDistance:0.01567855942887398},状态:“INIT”};嵌套异常是com.mongodb.MongoException:找不到任何特殊索引:2d(需要索引),2dsphere(需要索引),for:{address.location:{$nearSphere:{x:-120.0,y:47.0},$maxDistance:0.01567855942887398},状态:“INIT”} 但是,在运行Mongodb 需要有2d和2d球体索引,但可以';我们不能创建这两个索引。为什么?,mongodb,spring-data-mongodb,Mongodb,Spring Data Mongodb,在对MongoDB地理数据查询进行单元测试期间,我收到以下错误消息: 找不到任何特殊索引:2d(需要索引),2d球体(需要索引),for:{address.location:{$nearSphere:{x:-120.0,y:47.0},$maxDistance:0.01567855942887398},状态:“INIT”};嵌套异常是com.mongodb.MongoException:找不到任何特殊索引:2d(需要索引),2dsphere(需要索引),for:{address.location
createIndex
命令后,我发现以下错误:
db.store.createIndex({“address.location”:“2d”})
需要位置对象,位置数组格式不正确
db.store.createIndex({“address.location”:“2dsphere”})
无法从对象提取地理关键点,几何体格式错误?:{键入:“点”,坐标:[47.399465,-122.2950165]}
文件数据的结构如下所示:
{
....
"address" : {
"street" : "Street One",
"city" : "One City",
"province" : "aProvince",
"country" : "aCountry",
"postalCode" : "91202",
"location" : {
"type" : "Point",
"coordinates" : [
47.5891279,
-122.0446183
]
}
}
我做错了什么?看起来你的“经度”和“纬度”的顺序是相反的,错误应该是告诉你一口井(至少在3.x系列中是这样的)。你有“纬度”和“经度”,所以把它颠倒过来:
{
“地址”:{
“街道”:“第一街”,
“城市”:“一个城市”,
“省”:“省”,
“国家”:“一个国家”,
“postalCode”:“91202”,
“地点”:{
“类型”:“点”,
“坐标”:[
-122.0446183,
47.5891279
]
}
}
}
索引创建时不会出错:
db.store.ensureIndex({“address.location”:“2dsphere”})
这是一个索引。无法在对象上创建索引,因此只能直接使用“坐标”:
db.store.ensureIndex({“address.location.coordinates”:“2d”})
它是“平面”的,因此GeoJSON格式不适用。看起来您的“经度”和“纬度”的顺序是相反的,错误应该告诉您一口井(至少在3.x系列中是这样)。你有“纬度”和“经度”,所以把它颠倒过来:
{
“地址”:{
“街道”:“第一街”,
“城市”:“一个城市”,
“省”:“省”,
“国家”:“一个国家”,
“postalCode”:“91202”,
“地点”:{
“类型”:“点”,
“坐标”:[
-122.0446183,
47.5891279
]
}
}
}
索引创建时不会出错:
db.store.ensureIndex({“address.location”:“2dsphere”})
这是一个索引。无法在对象上创建索引,因此只能直接使用“坐标”:
db.store.ensureIndex({“address.location.coordinates”:“2d”})
它是“平面”的,因此GeoJSON格式不适用。非常感谢您提供的信息。我通过以下步骤解决了问题:1)升级到3.0.4;2) 转换lat和lng;3) 运行db.store.createIndex({“address.location.coordinates”:“2dsphere”})和db.store.createIndex({“address.location.coordinates”:“2d”})。非常感谢您提供的信息。我通过以下步骤解决了问题:1)升级到3.0.4;2) 转换lat和lng;3) 运行db.store.createIndex({“address.location.coordinates”:“2dsphere”})和db.store.createIndex({“address.location.coordinates”:“2d”})。