如何在 Mongoose 模式中表示 MongoDB GeoJSON 字段? [英] How does one represent MongoDB GeoJSON fields in a Mongoose Schema?

查看:22
本文介绍了如何在 Mongoose 模式中表示 MongoDB GeoJSON 字段?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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]]]
}

因此在 Mongoose 中,人们会认为架构将被定义为:

So in Mongoose one would think the schema would be defined like:

loc: { type: 'string', coordinates: [[['number']]] }

但这存在两个问题:

  1. 有一个名为type"的字段会破坏 Mongoose 的模式解析因为它允许在表单字段中定义字段:{ type: ,索引:} 等

  1. having a field called "type" screws up Mongoose's schema parsing because it allows defining fields in the form field: { type: , index: } etc.

Mongoose 不喜欢嵌套数组.

Mongoose does not like nested arrays.

克服这个问题的一种方法是简单地使用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

在此处查看 mongoose 测试以获取更多想法: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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆