模式中的mongoose geojson,“无法提取地理位置键";错误 [英] mongoose geojson in schema, "Can't extract geo keys" error

查看:123
本文介绍了模式中的mongoose geojson,“无法提取地理位置键";错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个具有已定义架构的mongodb集合,并且更新了该架构以包括经/纬度坐标.

I have a mongodb collection with a defined schema, and I updated this schema to include lat/lon coordinates.

旧版本:

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 comes from mongoose-geojson-schema and looks like this:

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

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

The collection already contained data before I added the location property.

显然现在发生的是由于某种原因,mongodb使用 {坐标:[],键入:"Point"} 作为现有文档的默认值,并且出现类似以下错误:

Apparently what happens now is that for some reason mongodb uses { coordinates: [], type: "Point" } as default value for the existing documents, and I get errors like these:

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

我已经研究了如何在模式中指定默认值,但是对于GeoJSON.Point数据类型,我看不到将值设置为 null 的方法.

I have looked at how to specify a default value in a schema, but I see no way of setting the value to null in case of a GeoJSON.Point data type.

我也尝试过

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

但这似乎也无济于事.

是因为 location 上的索引吗?

推荐答案

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

I would suggest you to not use GeoJSON.Point as a schema type. Just use mixed object type and everything will work properly.

location: Schema.Types.Mixed

这篇关于模式中的mongoose geojson,“无法提取地理位置键";错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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