【发布时间】:2014-07-28 15:40:52
【问题描述】:
我需要用这种结构查询一个 MongoDb 集合:
{
"_id" : ObjectId("53cfc8bf8f11381e28373153"),
"name" : "0700452",
"description" : "",
"styleurl" : "#style00001",
"point" : {
"altitudemode" : "clampToGround",
"coordinates" : "11.8263919657018, 41.2129293739724, 1.02757364822173"
}
}
我需要做的查询类型是基于 2dsphere 索引搜索,但是当我尝试这个时:
db.coordinatas.find({ $near : { type:'Point', point:{coordinates:"15.8263919657018, 41.2129293739724"}, $maxDistance : 100 }});
我得到一个错误:
error: {
"$err" : "can't find any special indices: 2d (needs index), 2dsphere (needs index), for: { $near: { type: \"Point\", point: { coordinates: \"15.8263919657018, 41.2129293739724\" }, $maxDistance: 100.0 } }",
"code" : 13038
}
与错误所说的索引相反:
[
{
"v" : 1,
"key" : {
"_id" : 1
},
"ns" : "test4-dev.coordinatas",
"name" : "_id_"
},
{
"v" : 1,
"key" : {
"coordinates" : "2dsphere"
},
"ns" : "test4-dev.coordinatas",
"name" : "coordinates"
}
]
这可能是因为 2dsphere 索引需要一个包含两个坐标的数组。
我在想也许我可以用这种新格式重组集合:
{
"_id" : ObjectId("53cfc8bf8f11381e28373153"),
"name" : "0700452",
"coordinates": [11.8263919657018, 41.2129293739724]
}
并以这种方式查询集合:
db.coordinatas.find({ $near : {type:'Point', coordinates:[11.8263919657018, 41.2129293739724], $maxDistance : 100 }});
如何在新集合中更改旧集合的格式?
【问题讨论】:
标签: mongodb