如何在 Mongoose 模式中表示 MongoDB GeoJSON 字段?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15556624/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 13:07:14  来源:igfitidea点击:

How does one represent MongoDB GeoJSON fields in a Mongoose Schema?

mongodbschemamongoosegeospatialgeojson

提问by Zugwalt

MongoDB 2.4 allows the use of GeoJSONobjects and a slew of neat functions and indexesthat I'd like to use.

MongoDB 2.4 允许使用GeoJSON对象以及我想使用的大量简洁的函数和索引

It expects GeoJSON objects to be stored in the format like:

它期望 GeoJSON 对象以如下格式存储:

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:

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

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

But this present two problems:

但这存在两个问题:

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

  2. Mongoose does not like nested arrays.

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

  2. Mongoose 不喜欢嵌套数组。

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!

克服这个问题的一种方法是简单地使用mongoose.Schema.Types.Mixed,但是我觉得必须有更好的方法!

采纳答案by aaronheckmann

You must used Mixed to represent arrays of arrays. There is an open ticketto support this in the future.

您必须使用 Mixed 来表示数组的数组。将来有一张公开票可以支持这一点。

@nevi_me is correct, you must declare the typeproperty as he described.

@nevi_me 是正确的,您必须type按照他的描述声明该属性。

Here's a gist: https://gist.github.com/aheckmann/5241574

这是一个要点:https: //gist.github.com/aheckmann/5241574

See the mongoose tests here for more ideas: https://github.com/LearnBoost/mongoose/blob/master/test/model.querying.test.js#L1931

有关更多想法,请参阅此处的猫鼬测试:https: //github.com/LearnBoost/mongoose/blob/master/test/model.querying.test.js#L1931

回答by Jed Watson

For reference, GeoJSON is officially supported in Mongoose 3.6

作为参考,Mongoose 3.6 正式支持 GeoJSON

See the release notes here.

请参阅此处的发行说明

Example (from the docs):

示例(来自文档):

new Schema({ loc: { type: [Number], index: '2dsphere'}})

... then ...

... 然后 ...

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)

回答by Mark Stosberg

The mongoose-geojson-schemapackage was created to make it easy to have GeoJSON in Mongoose Schemas.

猫鼬,以GeoJSON-架构包是为了让您轻松拥有以GeoJSON在猫鼬架构。

回答by arg20

Mongoose now officially supports this.

Mongoose 现在正式支持这一点

In a nutshell, what you do is, for that schema, you use the typeKeysetting to tell mongoose to use a different key for type information. Here is an example:

简而言之,您所做的是,对于该模式,您使用typeKey设置告诉猫鼬使用不同的键来获取类型信息。下面是一个例子:

var schema = new Schema({
  // Mongoose interpets this as 'loc is an object with 2 keys, type and coordinates'
  loc: { type: String, coordinates: [Number] },
  // Mongoose interprets this as 'name is a String'
  name: { $type: String }
}, { typeKey: '$type' }); // A '$type' key means this object is a type declaration

So now instead of declaring type info with the typeproperty, you use $type. This works at the schema level so use it in the schemas that need it.

因此,现在不是type使用属性声明类型信息,而是使用$type. 这适用于模式级别,因此在需要它的模式中使用它。

回答by nevi_me

I'm about to start moving all my location references in my MongoDB from '2d'to GeoJSON, so I'll encounter the same problem.

我即将开始将我在 MongoDB 中的所有位置引用从'2d'GeoJSON 移动,所以我会遇到同样的问题。

  • Regarding the typeproblem, you have to follow what I did below to get it working. Mongoose correctly recognises it as a string.
  • Nested arrays; I agree that mongoose.Schema.Types.Mixedwill work, but I think you can try what I did below, let me know if it works. I'm not near a PC with mongo installed to try the schema out.
  • 关于这个type问题,你必须按照我在下面做的来让它工作。Mongoose 正确地将其识别为字符串。
  • 嵌套数组;我同意这mongoose.Schema.Types.Mixed会奏效,但我认为你可以尝试我在下面所做的,让我知道它是否有效。我没有靠近安装了 mongo 的 PC 来尝试架构。

Here's how I'd define the schema. The nested array can be tweaked to work, so let me know if it doesn't.

这是我定义架构的方式。可以调整嵌套数组以使其工作,所以如果没有,请告诉我。

var LocationObject = new Schema ({
  'type': {
    type: String,
    required: true,
    enum: ['Point', 'LineString', 'Polygon'],
    default: 'Point'
  },
  coordinates: [
    [
      { type: [ Number ]
    ]
  ]
});

If you get undesired results in the nesting of the Array, try this out instead. Basically nesting in deeper.

如果您在 的嵌套中得到不想要的结果Array,请改用此方法。基本上嵌套更深。

coordinates: [
  { type: [
    { type: [ Number ] }
  ] }
]