如下所示,我尝试了在Polygon坐标周围使用2和3 []的不同尝试。
MongoDB版本:2.6.3 猫鼬版本:3.8.13
也许有人可以帮我找到类型或其他一些我看不到的愚蠢错误;-( 可悲的是,错误消息“未定义不是一个函数”并没有多大帮助。
谢谢!
猫鼬-模式
(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);
};
})();
集合中的示例文档
{
"_id": ObjectId("53d0d30c92a82799d8ed31d2"),
"loc": {
"type": "Point",
"coordinates": {
"0": 8.5652166666667,
"1": 49.3288
}
}
}
可运行的Mongo-Shell脚本 基于: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。 Mongoose尝试失败 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。使用2 [] 的Mongoose尝试失败 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.失败的Mongoose尝试 错误消息:MongoError:无法规范化查询:BadValue错误的地理位置查询 - 试图使用2 []左右坐标
来使用完整的GeoJSONvar 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。 Mongoose尝试失败 Errormessage:TypeError:undefined不是函数 - 试图使用3 []左右坐标
来使用完整的GeoJSONvar 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()返回
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)
答案 0 :(得分:2)
文档中的GeoJSON结构似乎不正确。尝试将坐标指定为数组:
{
"_id": ObjectId("53d0d30c92a82799d8ed31d2"),
"loc": {
"type": "Point",
"coordinates": [8.5652166666667, 49.3288]
}
}
这与你的第五个例子相结合应该可行。另请查看Point和Polygon上有关其结构的GeoJSON参考。
工作示例代码:
var mongoose = require('mongoose');
mongoose.connect('mongodb://localhost/test');
var PointSchema = new mongoose.Schema({
_id: mongoose.Schema.ObjectId,
loc: {
type: { type: String },
coordinates: { type: [Number] }
}
}, { collection: "points" });
PointSchema.index({ loc: "2dsphere" });
mongoose.model("Point", PointSchema);
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);
console.log(result);
});