使用 mongoose/mongodb 搜索点附近的所有多边形时失败
Failure when searching for all polygons near a point using mongoose/mongodb
我正在尝试搜索距某点 x 公里数的任何 GeoJSON 多边形,该多边形按与该点的距离排序。重要的是它是 GeoJSON 多边形而不是 GeoJSON 点。
我有以下内容,但出现错误:
'Unable to execute query: error processing query: ns=geoPolygonExample.areas limit=1000 skip=0\nTree: GEONEAR field=loc.coordinates maxdist=0.0156961 isNearSphere=1\nSort: {}\nProj: {}\n planner returned error: unable to find index for $geoNear query'
我已经检查过我的索引是否正确,并且我在模式更改之间删除了我的数据库,但无法弄清楚这一点。我是 运行 的代码在下面,可以独立运行。
/* Schema */
var mongoose = require('mongoose')
, Schema = mongoose.Schema
var ObjectId = Schema.ObjectId;
var AreaSchema = new Schema({
name: String,
loc: {
type : { type : String, default : 'Polygon' },
coordinates: []
}
})
AreaSchema.index({coordinates: '2dsphere'});
mongoose.model('Area', AreaSchema)
var Area = mongoose.model('Area')
/* Mongo connect */
var conn = mongoose.connect('mongodb://localhost/geoPolygonExample')
// Error handler
mongoose.connection.on('error', function (err) {
console.log(err);
});
/* Test case */
var my_area = new Area({
"name": "Test",
"loc": {
"coordinates": [[
[ -85.56045071399187, 38.215601 ],
[ -85.56080428954273, 38.21842155313534 ],
[ -85.5618514284852, 38.221133713995485 ],
[ -85.56045071399187, 38.215601 ]
]],
"type" : "Polygon"
}
});
my_area.save( function (err) {
Area
.find({})
.where('loc.coordinates')
.near({
center: [ -85.56045071399187, 38.215601 ],
maxDistance: 100/6371,
spherical: true
})
.exec(function(err, areas){
console.log("err: ", err);
console.log("areas: ", areas);
//* result: err: { [MongoError: n/a]
//* name: 'MongoError',
//* message: 'n/a',
//* '$err': 'Unable to execute query: error processing query: ns=geoPolygonExample.areas limit=1000 skip=0\nTree: GEONEAR field=loc.coordinates maxdist=0.0156961 isNearSphere=0\nSort: {}\nProj: {}\n planner returned error: unable to find index for $geoNear query',
//* code: 17007 }
//* areas: undefined
});
})
2dshpere
索引应该在 loc
属性 上创建,而不是 coordinates
属性。尝试将索引语句更改为:
AreaSchema.index({loc: '2dsphere'});
此外,索引是在后台创建的,有时需要一段时间才能生成,具体取决于集合中现有数据的数量。由于您的 insert
和 find
命令在模型注册后立即 运行ning 您可能会在第一次 运行 此代码时收到错误,因为索引很可能会尚不存在。
我正在尝试搜索距某点 x 公里数的任何 GeoJSON 多边形,该多边形按与该点的距离排序。重要的是它是 GeoJSON 多边形而不是 GeoJSON 点。
我有以下内容,但出现错误:
'Unable to execute query: error processing query: ns=geoPolygonExample.areas limit=1000 skip=0\nTree: GEONEAR field=loc.coordinates maxdist=0.0156961 isNearSphere=1\nSort: {}\nProj: {}\n planner returned error: unable to find index for $geoNear query'
我已经检查过我的索引是否正确,并且我在模式更改之间删除了我的数据库,但无法弄清楚这一点。我是 运行 的代码在下面,可以独立运行。
/* Schema */
var mongoose = require('mongoose')
, Schema = mongoose.Schema
var ObjectId = Schema.ObjectId;
var AreaSchema = new Schema({
name: String,
loc: {
type : { type : String, default : 'Polygon' },
coordinates: []
}
})
AreaSchema.index({coordinates: '2dsphere'});
mongoose.model('Area', AreaSchema)
var Area = mongoose.model('Area')
/* Mongo connect */
var conn = mongoose.connect('mongodb://localhost/geoPolygonExample')
// Error handler
mongoose.connection.on('error', function (err) {
console.log(err);
});
/* Test case */
var my_area = new Area({
"name": "Test",
"loc": {
"coordinates": [[
[ -85.56045071399187, 38.215601 ],
[ -85.56080428954273, 38.21842155313534 ],
[ -85.5618514284852, 38.221133713995485 ],
[ -85.56045071399187, 38.215601 ]
]],
"type" : "Polygon"
}
});
my_area.save( function (err) {
Area
.find({})
.where('loc.coordinates')
.near({
center: [ -85.56045071399187, 38.215601 ],
maxDistance: 100/6371,
spherical: true
})
.exec(function(err, areas){
console.log("err: ", err);
console.log("areas: ", areas);
//* result: err: { [MongoError: n/a]
//* name: 'MongoError',
//* message: 'n/a',
//* '$err': 'Unable to execute query: error processing query: ns=geoPolygonExample.areas limit=1000 skip=0\nTree: GEONEAR field=loc.coordinates maxdist=0.0156961 isNearSphere=0\nSort: {}\nProj: {}\n planner returned error: unable to find index for $geoNear query',
//* code: 17007 }
//* areas: undefined
});
})
2dshpere
索引应该在 loc
属性 上创建,而不是 coordinates
属性。尝试将索引语句更改为:
AreaSchema.index({loc: '2dsphere'});
此外,索引是在后台创建的,有时需要一段时间才能生成,具体取决于集合中现有数据的数量。由于您的 insert
和 find
命令在模型注册后立即 运行ning 您可能会在第一次 运行 此代码时收到错误,因为索引很可能会尚不存在。