多边形内的Mongoose Geo-Query

时间:2014-07-25 18:59:31

标签: node.js mongodb mongoose geospatial

不知怎的,我疯了,但让我解释一下。 我有一个文档集合,其中每个文档在“loc”字段中具有GPS坐标(下面的示例)。现在我想查询特定区域中的所有文档。 当我使用MongoShell进行查询时,我得到了正确的结果,但是当我使用Mongoose尝试它时,每次尝试都会失败。

如下所示,我尝试了在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/2092http://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 []左右坐标

来使用完整的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。 Mongoose尝试失败 Errormessage:TypeError:undefined不是函数 - 试图使用3 []左右坐标

来使用完整的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()返回

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 个答案:

答案 0 :(得分:2)

文档中的GeoJSON结构似乎不正确。尝试将坐标指定为数组:

 {
   "_id": ObjectId("53d0d30c92a82799d8ed31d2"),
   "loc": {
     "type": "Point",
     "coordinates": [8.5652166666667, 49.3288]
  }
}

这与你的第五个例子相结合应该可行。另请查看PointPolygon上有关其结构的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);
});