MongoDB 2.4 允许使用 GeoJSON对象和大量 neat functions and indexes我想用。
它期望 GeoJSON 对象以如下格式存储:
loc: {
type: 'Polygon',
coordinates: [[[-180.0, 10.0], [20.0, 90.0], [180.0, -5.0], [-30.0, -90.0]]]
}
所以在 Mongoose 中,人们会认为架构的定义如下:
loc: { type: 'string', coordinates: [[['number']]] }
但这会带来两个问题:
有一个名为“type”的字段会破坏 Mongoose 的模式解析 因为它允许在表单字段中定义字段:{ type: , 索引:}等。
Mongoose 不喜欢嵌套数组。
解决这个问题的一种方法是简单地使用 mongoose.Schema.Types.Mixed
,但是我觉得必须有更好的方法!
最佳答案
供引用,Mongoose 3.6 正式支持 GeoJSON
See the release notes here .
示例(来自文档):
new Schema({ loc: { type: [Number], index: '2dsphere'}})
...然后...
var geojsonPoly = { type: 'Polygon', coordinates: [[[-5,-5], ['-5',5], [5,5], [5,-5],[-5,'-5']]] }
Model.find({ loc: { $within: { $geometry: geojsonPoly }}})
// or
Model.where('loc').within.geometry(geojsonPoly)
关于mongodb - 如何在 Mongoose Schema 中表示 MongoDB GeoJSON 字段?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15556624/