不知怎的,我疯了,但让我解释一下 . 我有一个文档集合,其中每个文档在“loc”字段中具有GPS坐标(下面的示例) . 现在我想查询特定区域中的所有文档 . 当我使用MongoShell进行查询时,我得到了正确的结果,但是当我使用Mongoose尝试它时,每次尝试都会失败 .
正如您在下面看到的,我尝试了在Polygon坐标周围使用2和3 []的不同尝试 .
MongoDB版本:2.6.3 Mongoose版本:3.8.13
也许有人可以帮我找到类型或其他一些我看不到的愚蠢错误;-(可悲的是,错误信息“未定义不是一个功能”并没有多大帮助 .
谢谢!
Mongoose-Schema
(function () {
module.exports = function (mongoose, Schema) {
var PointSchema = new Schema({
_id: Schema.ObjectId,
loc: {
type: { type: String },
coordinates: { type: [Number]}
}
}, { collection: "points" });
PointSchema.index({ loc: "2dsphere" });
mongoose.model("Point", PointSchema);
};
})();
Sample document from the collection
{
"_id": ObjectId("53d0d30c92a82799d8ed31d2"),
"loc": {
"type": "Point",
"coordinates": {
"0": 8.5652166666667,
"1": 49.3288
}
}
}
Mongo-Shell script which works 基于:http://docs.mongodb.org/manual/reference/operator/query/geoWithin/#op._S_geoWithin
db.points.find( { loc :
{ $geoWithin :
{ $geometry :
{ type : "Polygon" ,
coordinates : [[[8.594874265234353, 49.33654935186479],
[8.594874265234353, 49.322858939564284],
[8.553675534765603, 49.322858939564284],
[8.553675534765603, 49.33654935186479],
[8.594874265234353, 49.33654935186479]]]
} } } } )
1. Failed Mongoose attempt Errormessage:TypeError:undefined不是函数 - 尝试获取shell脚本并将其放入"find"
var Point = mongoose.model("Point");
var pointFields = { '_id': 1, 'loc': 1 };
Point.find({
loc:
{
$geoWithin:
{
$geometry:
{
type: "Polygon",
coordinates: [[[8.594874265234353, 49.33654935186479],
[8.594874265234353, 49.322858939564284],
[8.553675534765603, 49.322858939564284],
[8.553675534765603, 49.33654935186479],
[8.594874265234353, 49.33654935186479]]]
}
}
}
}).select(pointFields).lean().exec(function (error, result) {
console.log("Error: " + error);
processResponse(error, result, response);
});
** 2 . 使用3 []失败的Mongoose尝试**错误消息:MongoError:无法规范化查询:BadValue错误的地理查询 - 基于:https://github.com/LearnBoost/mongoose/issues/2092
var geoJsonPoly = {
polygon: [[[8.594874265234353, 49.33654935186479],
[8.594874265234353, 49.322858939564284],
[8.553675534765603, 49.322858939564284],
[8.553675534765603, 49.33654935186479],
[8.594874265234353, 49.33654935186479]]]
};
var Point = mongoose.model("Point");
var pointFields = { '_id': 1, 'loc': 1 };
Point.find({}).where('loc').within(geoJsonPoly).select(pointFields).lean().exec(function (error, result) {
console.log("Error: " + error);
processResponse(error, result, response);
});
3. Failed Mongoose attempt using 2[] Errormessage:TypeError:undefined不是函数基于:https://github.com/LearnBoost/mongoose/issues/2092和http://mongoosejs.com/docs/api.html#query_Query-within
var geoJsonPoly = {
polygon: [[8.594874265234353, 49.33654935186479],
[8.594874265234353, 49.322858939564284],
[8.553675534765603, 49.322858939564284],
[8.553675534765603, 49.33654935186479],
[8.594874265234353, 49.33654935186479]]
};
var Point = mongoose.model("Point");
var pointFields = { '_id': 1, 'loc': 1 };
Point.find({}).where('loc').within(geoJsonPoly).select(pointFields).lean().exec(function (error, result) {
console.log("Error: " + error);
processResponse(error, result, response);
});
4.Failed Mongoose attempt Errormessage:MongoError:无法规范化查询:BadValue错误的地理查询 - 尝试使用2 [] around坐标使用完整的GeoJSON
var geoJsonPoly = {
type: "Polygon",
coordinates: [[8.594874265234353, 49.33654935186479],
[8.594874265234353, 49.322858939564284],
[8.553675534765603, 49.322858939564284],
[8.553675534765603, 49.33654935186479],
[8.594874265234353, 49.33654935186479]]
};
var Point = mongoose.model("Point");
var pointFields = { '_id': 1, 'loc': 1 };
Point.find({}).where('loc').within(geoJsonPoly).select(pointFields).lean().exec(function (error, result) {
console.log("Error: " + error);
processResponse(error, result, response);
});
5. Failed Mongoose attempt Errormessage:TypeError:undefined不是函数 - 尝试使用3 [] around坐标使用完整的GeoJSON
var geoJsonPoly = {
type: "Polygon",
coordinates: [[[8.594874265234353, 49.33654935186479],
[8.594874265234353, 49.322858939564284],
[8.553675534765603, 49.322858939564284],
[8.553675534765603, 49.33654935186479],
[8.594874265234353, 49.33654935186479]]]
};
var Point = mongoose.model("Point");
var pointFields = { '_id': 1, 'loc': 1 };
Point.find({}).where('loc').within(geoJsonPoly).select(pointFields).lean().exec(function (error, result) {
console.log("Error: " + error);
processResponse(error, result, response);
});
console.trace() returns
Trace
at Promise.<anonymous> (C:\Dev\Mobile\src\nodejs\analyze.js:336:17)
at Promise.<anonymous> (C:\Dev\Mobile\src\nodejs\node_modules\mongoose\node_modules\mpromise\lib\promise.js:172:8)
at Promise.EventEmitter.emit (events.js:95:17)
at Promise.emit (C:\Dev\Mobile\src\nodejs\node_modules\mongoose\node_modules\mpromise\lib\promise.js:84:38)
at Promise.reject (C:\Dev\Mobile\src\nodejs\node_modules\mongoose\node_modules\mpromise\lib\promise.js:111:15)
at Promise.error (C:\Dev\Mobile\src\nodejs\node_modules\mongoose\lib\promise.js:89:15)
at Promise.resolve (C:\Dev\Mobile\src\nodejs\node_modules\mongoose\lib\promise.js:107:24)
at Promise.<anonymous> (C:\Dev\Mobile\src\nodejs\node_modules\mongoose\node_modules\mpromise\lib\promise.js:172:8)
at Promise.EventEmitter.emit (events.js:95:17)
at Promise.emit (C:\Dev\Mobile\src\nodejs\node_modules\mongoose\node_modules\mpromise\lib\promise.js:84:38)
1 回答
文档中的GeoJSON结构似乎不正确 . 尝试将坐标指定为数组:
这与你的第五个例子相结合应该可行 . 还要检查Point和Polygon上关于其结构的GeoJSON引用 .
工作示例代码: