【问题标题】:mongoose geojson in schema, "Can't extract geo keys" error模式中的猫鼬 geojson,“无法提取地理键”错误
【发布时间】:2016-03-05 13:49:12
【问题描述】:

我有一个带有定义架构的 mongodb 集合,我更新了这个架构以包含纬度/经度坐标。

旧版本:

var schema = mongoose.Schema({
    id: String,
    name: String,
    address: String,
    city: String,
    zip: String,
    country: String,
    phoneNumber: String,
    mobile: String,
    website: String,
    email: String,
});

新版本

var schema = mongoose.Schema({
    id: String,
    name: String,
    address: String,
    city: String,
    zip: String,
    country: String,
    phoneNumber: String,
    mobile: String,
    website: String,
    email: String,
    location: GeoJSON.Point
});

schema.index({ location: '2dsphere' });

GEOJSON.Point 来自mongoose-geojson-schema,看起来像这样:

GeoJSON.Point = {
  'type'     : { type: String, default: "Point" },
  coordinates: [
    {type: "Number"}
  ]
}

在我添加 location 属性之前,该集合已经包含数据。

显然现在发生的事情是由于某种原因 mongodb 使用 { coordinates: [], type: "Point" } 作为现有文档的默认值,我收到如下错误:

 MongoError: Can't extract geo keys: { _id: ObjectId('5637ea3ca5b2613f37d826f6'), ...
 Point must only contain numeric elements 

我查看了如何在架构中指定默认值,但我看不到在 GeoJSON.Point 数据类型的情况下将值设置为 null

我也试过

db.collection.update({},{$set:{location:null},{multi:true})

但这似乎也没有帮助。

是不是因为location上的索引?

【问题讨论】:

  • Mongoose 3.6 release notes 建议您使用数组作为类型。 GeoJSON.Point 来自哪里?
  • 我用额外的信息编辑了我的问题

标签: node.js mongodb mongoose geojson


【解决方案1】:

我认为您需要使用适当的架构将 GeoJSON.Point 升级到 sub document

GeoJSON.Point = new mongoose.Schema({
  'type'     : { type: String, default: "Point" },
  coordinates: [ { type: "Number" } ]
});

结合默认启用的minimize 选项,这将使Mongoose 仅​​在实际设置时保存location 属性。

【讨论】:

    【解决方案2】:

    我建议您不要使用 GeoJSON.Point 作为架构类型。只需使用混合对象类型,一切都会正常工作。

    location: Schema.Types.Mixed
    

    【讨论】:

      猜你喜欢
      • 2015-04-29
      • 2015-05-25
      • 1970-01-01
      • 2017-04-17
      • 1970-01-01
      • 2014-12-04
      • 2017-05-03
      • 2019-06-21
      • 2017-08-01
      相关资源
      最近更新 更多