架构中的 mongoose geojson,“无法提取地理密钥”错误

2024-04-06

我有一个具有已定义架构的 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 

我已经研究了如何在架构中指定默认值,但我看不到如何将该值设置为null对于 GeoJSON.Point 数据类型。

我也尝试过

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

但这似乎也没有帮助。

是不是因为上面的索引location?


我认为你需要升级GeoJSON.Point to a 子文档 http://mongoosejs.com/docs/subdocs.html具有适当的架构:

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

结合minimize http://mongoosejs.com/docs/guide.html#minimize选项,默认情况下启用,这将使 Mongoose 只保存location属性(如果实际已设置)。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

架构中的 mongoose geojson,“无法提取地理密钥”错误 的相关文章

随机推荐