【问题标题】:How does one represent MongoDB GeoJSON fields in a Mongoose Schema?如何在 Mongoose Schema 中表示 MongoDB GeoJSON 字段?
【发布时间】:2013-03-11 11:59:50
【问题描述】:

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

但这会带来两个问题:

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

  2. Mongoose 不喜欢嵌套数组。

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

【问题讨论】:

    标签: mongodb schema mongoose geospatial geojson


    【解决方案1】:

    猫鼬现在officially supports this

    简而言之,您所做的是,对于该架构,您使用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
    

    因此,现在您不再使用 type 属性声明类型信息,而是使用 $type。这适用于架构级别,因此请在需要它的架构中使用它。

    【讨论】:

    【解决方案2】:

    创建mongoose-geojson-schema 包是为了让在 Mongoose Schemas 中轻松使用 GeoJSON。

    【讨论】:

      【解决方案3】:

      供参考,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)
      

      【讨论】:

      • 如何使用此方案保存 GeoJson 点?
      • 您只需将loc 路径的值设置为这样的数组:item.loc = [lng, lat]; item.save()
      • 谢谢,但是类型是如何定义的呢?就像我需要定义多边形、线或多点一样?
      • 必须是Model.where('loc').within()...
      【解决方案4】:

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

      • 关于type 问题,您必须按照我在下面所做的操作才能使其正常工作。 Mongoose 正确地将其识别为字符串。
      • 嵌套数组;我同意mongoose.Schema.Types.Mixed 会起作用,但我想你可以试试我在下面所做的,如果它有效,请告诉我。我不在安装了 mongo 的 PC 附近来试用该架构。

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

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

      如果您在 Array 的嵌套中得到不希望的结果,请尝试使用此方法。基本上嵌套更深。

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

      【讨论】:

        【解决方案5】:

        您必须使用 Mixed 来表示数组的数组。将来有一个open ticket 来支持它。

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

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

        在此处查看猫鼬测试以获取更多想法:https://github.com/LearnBoost/mongoose/blob/master/test/model.querying.test.js#L1931

        【讨论】:

        • 谢谢!很高兴在 Mongoose 文档中看到对此的解释。
        猜你喜欢
        • 2018-03-11
        • 2016-06-03
        • 2012-11-13
        • 1970-01-01
        • 2018-12-30
        • 2019-12-09
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多