一个如何在Mongoose模式中表示MongoDB GeoJSON字段? [英] How does one represent MongoDB GeoJSON fields in a Mongoose Schema?
问题描述
MongoDB 2.4允许使用 GeoJSON 对象和大量
MongoDB 2.4 allows the use of GeoJSON objects and a slew of neat functions and indexes that I'd like to use.
它希望GeoJSON对象以以下格式存储:
It expects GeoJSON objects to be stored in the format like:
loc: {
type: 'Polygon',
coordinates: [[[-180.0, 10.0], [20.0, 90.0], [180.0, -5.0], [-30.0, -90.0]]]
}
因此,在猫鼬中,人们会认为该模式将被定义为:
So in Mongoose one would think the schema would be defined like:
loc: { type: 'string', coordinates: [[['number']]] }
但这会带来两个问题:
-
具有名为"type"的字段会破坏Mongoose的模式解析 因为它允许在表单字段中定义字段:{type:, 索引:}等.
having a field called "type" screws up Mongoose's schema parsing because it allows defining fields in the form field: { type: , index: } etc.
猫鼬不喜欢嵌套数组.
克服此问题的一种方法是简单地使用mongoose.Schema.Types.Mixed
,但是我觉得必须有更好的方法!
One way to overcome this is to simply use mongoose.Schema.Types.Mixed
, however I feel that there has got to be a better way!
推荐答案
您必须使用Mixed来表示数组数组.有公开票证,将来会对此提供支持.
You must used Mixed to represent arrays of arrays. There is an open ticket to support this in the future.
@nevi_me是正确的,您必须按照他的描述声明type
属性.
@nevi_me is correct, you must declare the type
property as he described.
这里是要点: https://gist.github.com/aheckmann/5241574
有关更多想法,请参见此处的猫鼬测试: https://github.com/LearnBoost/mongoose/blob/master/test/model.querying.test.js#L1931
See the mongoose tests here for more ideas: https://github.com/LearnBoost/mongoose/blob/master/test/model.querying.test.js#L1931
这篇关于一个如何在Mongoose模式中表示MongoDB GeoJSON字段?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!